diff options
author | Betsy McPhail <betsy.mcphail@kitware.com> | 2019-01-20 16:28:39 (GMT) |
---|---|---|
committer | Betsy McPhail <betsy.mcphail@kitware.com> | 2019-01-27 21:03:00 (GMT) |
commit | f2ddedfa5802c8aece994bb643b5139212d87777 (patch) | |
tree | f1dc800bf22bbfccbde4e1deb0c235986731e2cd /Tests/Tutorial/Step1 | |
parent | 438651506a5417be70e71f54f4ed7add0c2604d3 (diff) | |
download | CMake-f2ddedfa5802c8aece994bb643b5139212d87777.zip CMake-f2ddedfa5802c8aece994bb643b5139212d87777.tar.gz CMake-f2ddedfa5802c8aece994bb643b5139212d87777.tar.bz2 |
Tests: Update CMake tutorial
Latest material from data.kitware.com -> Collections -> Courses -> CMake.
Diffstat (limited to 'Tests/Tutorial/Step1')
-rw-r--r-- | Tests/Tutorial/Step1/CMakeLists.txt | 19 | ||||
-rw-r--r-- | Tests/Tutorial/Step1/directions.txt | 96 | ||||
-rw-r--r-- | Tests/Tutorial/Step1/tutorial.cxx | 17 |
3 files changed, 106 insertions, 26 deletions
diff --git a/Tests/Tutorial/Step1/CMakeLists.txt b/Tests/Tutorial/Step1/CMakeLists.txt index e461d3c..141f0c2 100644 --- a/Tests/Tutorial/Step1/CMakeLists.txt +++ b/Tests/Tutorial/Step1/CMakeLists.txt @@ -1,20 +1,3 @@ -cmake_minimum_required (VERSION 2.6) -project (Tutorial) +project(Tutorial) -# The version number. -set (Tutorial_VERSION_MAJOR 1) -set (Tutorial_VERSION_MINOR 0) - -# configure a header file to pass some of the CMake settings -# to the source code -configure_file ( - "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" - "${PROJECT_BINARY_DIR}/TutorialConfig.h" - ) - -# add the binary tree to the search path for include files -# so that we will find TutorialConfig.h -include_directories("${PROJECT_BINARY_DIR}") - -# add the executable add_executable(Tutorial tutorial.cxx) diff --git a/Tests/Tutorial/Step1/directions.txt b/Tests/Tutorial/Step1/directions.txt new file mode 100644 index 0000000..10f2583 --- /dev/null +++ b/Tests/Tutorial/Step1/directions.txt @@ -0,0 +1,96 @@ +# Adding a Version Number and Configured Header File # + +The first feature we will add is to provide our executable and project with a +version number. While we could do this exclusively in the source code, using +CMakeLists provides more flexibility. + +To add a version number we modify the CMakeLists file as follows: + + cmake_minimum_required(VERSION 3.3) + project(Tutorial) + + # the version number. + set(Tutorial_VERSION_MAJOR 1) + set(Tutorial_VERSION_MINOR 0) + + # configure a header file to pass some of the CMake settings + # to the source code + configure_file( + "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" + "${PROJECT_BINARY_DIR}/TutorialConfig.h" + ) + + # add the executable + add_executable(Tutorial tutorial.cxx) + + # add the binary tree to the search path for include files + # so that we will find TutorialConfig.h + target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + + +We then create a TutorialConfig.h.in file in the source tree with the +following contents: + + // the configured options and settings for Tutorial + #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ + #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ + +When CMake configures this header file the values for @Tutorial_VERSION_MAJOR@ +and @Tutorial_VERSION_MINOR@ will be replaced by the values from the CMakeLists +file. Next we modify tutorial.cxx to include the configured header file and to +make use of the version numbers. The resulting source code is listed below. + + // A simple program that computes the square root of a number + #include <cmath> + #include <iostream> + #include <string> + #include <sstream> + + #include "TutorialConfig.h" + + int main (int argc, char *argv[]) + { + if (argc < 2) + { + std::cout << argv[0] << " Version " + << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR + << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + double inputValue = atof(argv[1]); + + double outputValue = sqrt(inputValue); + std::cout << "The square root of " + << inputValue << " is " << outputValue << std::endl; + return 0; + } + +# Adding C++11 support # + +Let's add some C++11 features to our project. We will need to explicitly state +in the CMake code that it should use the correct flags. The easiest way to +enable C++11 support for CMake is by using the CMAKE_CXX_STANDARD +and CMAKE_CXX_STANDARD_REQUIRED variables. + +First, replace `atof` with `std::stod` in tutorial.cxx. + +Then, add the CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED variables to +the CMakeLists file. The STANADARD value should be set to 11, and REQUIRED +should be set to True. + + +# Build and Test # + +Run cmake or cmake-gui to configure the project and then build it with your +chosen build tool + +cd to the directory where Tutorial was built (likely the make directory or +a Debug or Release build configuration subdirectory) and run these commands: + + Tutorial 4294967296 + Tutorial 10 + Tutorial diff --git a/Tests/Tutorial/Step1/tutorial.cxx b/Tests/Tutorial/Step1/tutorial.cxx index 7a13376..f8dd0c6 100644 --- a/Tests/Tutorial/Step1/tutorial.cxx +++ b/Tests/Tutorial/Step1/tutorial.cxx @@ -1,19 +1,20 @@ // A simple program that computes the square root of a number -#include "TutorialConfig.h" -#include <math.h> -#include <stdio.h> -#include <stdlib.h> +#include <cmath> +#include <cstdlib> +#include <iostream> +#include <string> int main(int argc, char* argv[]) { if (argc < 2) { - fprintf(stdout, "%s Version %d.%d\n", argv[0], Tutorial_VERSION_MAJOR, - Tutorial_VERSION_MINOR); - fprintf(stdout, "Usage: %s number\n", argv[0]); + std::cout << "Usage: " << argv[0] << " number" << std::endl; return 1; } + double inputValue = atof(argv[1]); + double outputValue = sqrt(inputValue); - fprintf(stdout, "The square root of %g is %g\n", inputValue, outputValue); + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; return 0; } |