summaryrefslogtreecommitdiffstats
path: root/Help/guide/tutorial/Step6
diff options
context:
space:
mode:
authorBetsy McPhail <betsy.mcphail@kitware.com>2019-06-18 14:49:40 (GMT)
committerBrad King <brad.king@kitware.com>2019-06-19 12:57:12 (GMT)
commiteef3e020c21d2fdba19aab0daf1b99f8de0a16fe (patch)
tree30c79204ed261551c611d6f1f159ef682c8b2b74 /Help/guide/tutorial/Step6
parent862cfc0e6c3f275db73281f3b9b989704251ab6a (diff)
downloadCMake-eef3e020c21d2fdba19aab0daf1b99f8de0a16fe.zip
CMake-eef3e020c21d2fdba19aab0daf1b99f8de0a16fe.tar.gz
CMake-eef3e020c21d2fdba19aab0daf1b99f8de0a16fe.tar.bz2
Help: Populate tutorial guide text
Migrate tutorial text from individual `directions.txt` files to the main tutorial document. Add some comments to source code to provide anchors for inclusion.
Diffstat (limited to 'Help/guide/tutorial/Step6')
-rw-r--r--Help/guide/tutorial/Step6/CMakeLists.txt2
-rw-r--r--Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt2
-rw-r--r--Help/guide/tutorial/Step6/directions.txt104
-rw-r--r--Help/guide/tutorial/Step6/tutorial.cxx3
4 files changed, 5 insertions, 106 deletions
diff --git a/Help/guide/tutorial/Step6/CMakeLists.txt b/Help/guide/tutorial/Step6/CMakeLists.txt
index a78b0ff..5829891 100644
--- a/Help/guide/tutorial/Step6/CMakeLists.txt
+++ b/Help/guide/tutorial/Step6/CMakeLists.txt
@@ -3,7 +3,7 @@ project(Tutorial)
set(CMAKE_CXX_STANDARD 14)
-# the version number.
+# set the version number
set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
diff --git a/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt
index 2946075..def1140 100644
--- a/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt
+++ b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt
@@ -4,11 +4,11 @@ add_library(MathFunctions mysqrt.cxx)
# to find MathFunctions.h, while we don't.
# state that we depend on Tutorial_BINARY_DIR but consumers don't, as the
# TutorialConfig.h include is an implementation detail
-
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${Tutorial_BINARY_DIR}
)
+# install rules
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)
diff --git a/Help/guide/tutorial/Step6/directions.txt b/Help/guide/tutorial/Step6/directions.txt
deleted file mode 100644
index 42b9f06..0000000
--- a/Help/guide/tutorial/Step6/directions.txt
+++ /dev/null
@@ -1,104 +0,0 @@
-# Adding a Custom Command and Generated File #
-
-In this section we will show how you can add a generated source file into the
-build process of an application. For this example, we will create a table of
-precomputed square roots as part of the build process, and then compile that
-table into our application.
-
-To accomplish this, we first need a program that will generate the table. In the
-MathFunctions subdirectory a new source file named MakeTable.cxx will do just that.
-
- // A simple program that builds a sqrt table
- #include <iostream>
- #include <fstream>
- #include <cmath>
-
- int main (int argc, char *argv[])
- {
- // make sure we have enough arguments
- if (argc < 2) {
- return 1;
- }
-
- std::ofstream fout(argv[1],std::ios_base::out);
- const bool fileOpen = fout.is_open();
- if(fileOpen) {
- fout << "double sqrtTable[] = {" << std::endl;
- for (int i = 0; i < 10; ++i) {
- fout << sqrt(static_cast<double>(i)) << "," << std::endl;
- }
- // close the table with a zero
- fout << "0};" << std::endl;
- fout.close();
- }
- return fileOpen ? 0 : 1; // return 0 if wrote the file
- }
-
-Note that the table is produced as valid C++ code and that the output filename
-is passed in as an argument.
-
-The next step is to add the appropriate commands to MathFunctions’ CMakeLists
-file to build the MakeTable executable and then run it as part of the build
-process. A few commands are needed to accomplish this, as shown below:
-
- # first we add the executable that generates the table
- add_executable(MakeTable MakeTable.cxx)
-
- # add the command to generate the source code
- add_custom_command(
- OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
- COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
- DEPENDS MakeTable
- )
-
- # add the main library
- add_library(MathFunctions
- mysqrt.cxx
- ${CMAKE_CURRENT_BINARY_DIR}/Table.h
- )
-
- target_include_directories(MathFunctions
- INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
- PUBLIC ${Tutorial_BINARY_DIR}
- # add the binary tree directory to the search path for include files
- ${CMAKE_CURRENT_BINARY_DIR}
- )
-
- install(TARGETS MathFunctions DESTINATION lib)
- install(FILES MathFunctions.h DESTINATION include)
-
-First, the executable for MakeTable is added as any other executable would be
-added. Then we add a custom command that specifies how to produce Table.h by
-running MakeTable. Next we have to let CMake know that mysqrt.cxx depends on
-the generated file Table.h. This is done by adding the generated Table.h to the
-list of sources for the library MathFunctions. We also have to add the current
-binary directory to the list of include directories so that Table.h can be
-found and included by mysqrt.cxx.
-
-Now let's use the generated table. First, modify mysqrt.cxx to include Table.h.
-Next, we can rewrite the mysqrt function to use the table:
-
- if (x <= 0) {
- return 0;
- }
-
- // use the table to help find an initial value
- double result = x;
- if (x >= 1 && x < 10) {
- result = sqrtTable[static_cast<int>(x)];
- }
-
- // do ten iterations
- for (int i = 0; i < 10; ++i) {
- if (result <= 0) {
- result = 0.1;
- }
- double delta = x - (result*result);
- result = result + 0.5*delta/result;
- std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
- }
-
-Run cmake or cmake-gui to configure the project and then build it with your
-chosen build tool. When this project is built it will first build the MakeTable
-executable. It will then run MakeTable to produce Table.h. Finally, it will
-compile mysqrt.cxx which includes Table.h to produce the MathFunctions library.
diff --git a/Help/guide/tutorial/Step6/tutorial.cxx b/Help/guide/tutorial/Step6/tutorial.cxx
index c2b89df..8156a9c 100644
--- a/Help/guide/tutorial/Step6/tutorial.cxx
+++ b/Help/guide/tutorial/Step6/tutorial.cxx
@@ -5,6 +5,7 @@
#include "TutorialConfig.h"
+// should we include the MathFunctions header?
#ifdef USE_MYMATH
# include "MathFunctions.h"
#endif
@@ -12,6 +13,7 @@
int main(int argc, char* argv[])
{
if (argc < 2) {
+ // report version
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
<< Tutorial_VERSION_MINOR << std::endl;
std::cout << "Usage: " << argv[0] << " number" << std::endl;
@@ -20,6 +22,7 @@ int main(int argc, char* argv[])
double inputValue = std::stod(argv[1]);
+ // which square root function should we use?
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
#else