directions.txt 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Adding a Version Number and Configured Header File #
  2. The first feature we will add is to provide our executable and project with a
  3. version number. While we could do this exclusively in the source code, using
  4. CMakeLists provides more flexibility.
  5. To add a version number we modify the CMakeLists file as follows:
  6. cmake_minimum_required(VERSION 3.3)
  7. project(Tutorial)
  8. # the version number.
  9. set(Tutorial_VERSION_MAJOR 1)
  10. set(Tutorial_VERSION_MINOR 0)
  11. # configure a header file to pass some of the CMake settings
  12. # to the source code
  13. configure_file(
  14. "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  15. "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  16. )
  17. # add the executable
  18. add_executable(Tutorial tutorial.cxx)
  19. # add the binary tree to the search path for include files
  20. # so that we will find TutorialConfig.h
  21. target_include_directories(Tutorial PUBLIC
  22. "${PROJECT_BINARY_DIR}"
  23. )
  24. We then create a TutorialConfig.h.in file in the source tree with the
  25. following contents:
  26. // the configured options and settings for Tutorial
  27. #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
  28. #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
  29. When CMake configures this header file the values for @Tutorial_VERSION_MAJOR@
  30. and @Tutorial_VERSION_MINOR@ will be replaced by the values from the CMakeLists
  31. file. Next we modify tutorial.cxx to include the configured header file and to
  32. make use of the version numbers. The resulting source code is listed below.
  33. // A simple program that computes the square root of a number
  34. #include <cmath>
  35. #include <iostream>
  36. #include <string>
  37. #include <sstream>
  38. #include "TutorialConfig.h"
  39. int main (int argc, char *argv[])
  40. {
  41. if (argc < 2) {
  42. std::cout << argv[0] << " Version "
  43. << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR
  44. << std::endl;
  45. std::cout << "Usage: " << argv[0] << " number" << std::endl;
  46. return 1;
  47. }
  48. double inputValue = atof(argv[1]);
  49. double outputValue = sqrt(inputValue);
  50. std::cout << "The square root of "
  51. << inputValue << " is " << outputValue << std::endl;
  52. return 0;
  53. }
  54. # Adding C++11 support #
  55. Let's add some C++11 features to our project. We will need to explicitly state
  56. in the CMake code that it should use the correct flags. The easiest way to
  57. enable C++11 support for CMake is by using the CMAKE_CXX_STANDARD
  58. and CMAKE_CXX_STANDARD_REQUIRED variables.
  59. First, replace `atof` with `std::stod` in tutorial.cxx.
  60. Then, add the CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED variables to
  61. the CMakeLists file. The STANADARD value should be set to 11, and REQUIRED
  62. should be set to True.
  63. # Build and Test #
  64. Run cmake or cmake-gui to configure the project and then build it with your
  65. chosen build tool
  66. cd to the directory where Tutorial was built (likely the make directory or
  67. a Debug or Release build configuration subdirectory) and run these commands:
  68. Tutorial 4294967296
  69. Tutorial 10
  70. Tutorial