A Basic Starting Point.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Step 1: A Basic Starting Point
  2. ==============================
  3. The most basic project is an executable built from source code files.
  4. For simple projects, a three line ``CMakeLists.txt`` file is all that is
  5. required. This will be the starting point for our tutorial. Create a
  6. ``CMakeLists.txt`` file in the ``Step1`` directory that looks like:
  7. .. code-block:: cmake
  8. :caption: CMakeLists.txt
  9. :name: CMakeLists.txt-start
  10. cmake_minimum_required(VERSION 3.10)
  11. # set the project name
  12. project(Tutorial)
  13. # add the executable
  14. add_executable(Tutorial tutorial.cxx)
  15. Note that this example uses lower case commands in the ``CMakeLists.txt`` file.
  16. Upper, lower, and mixed case commands are supported by CMake. The source
  17. code for ``tutorial.cxx`` is provided in the ``Step1`` directory and can be
  18. used to compute the square root of a number.
  19. Adding a Version Number and Configured Header File
  20. --------------------------------------------------
  21. The first feature we will add is to provide our executable and project with a
  22. version number. While we could do this exclusively in the source code, using
  23. ``CMakeLists.txt`` provides more flexibility.
  24. First, modify the ``CMakeLists.txt`` file to use the :command:`project` command
  25. to set the project name and version number.
  26. .. literalinclude:: Step2/CMakeLists.txt
  27. :caption: CMakeLists.txt
  28. :name: CMakeLists.txt-project-VERSION
  29. :language: cmake
  30. :end-before: # specify the C++ standard
  31. Then, configure a header file to pass the version number to the source
  32. code:
  33. .. literalinclude:: Step2/CMakeLists.txt
  34. :caption: CMakeLists.txt
  35. :name: CMakeLists.txt-configure_file
  36. :language: cmake
  37. :start-after: # to the source code
  38. :end-before: # add the executable
  39. Since the configured file will be written into the binary tree, we
  40. must add that directory to the list of paths to search for include
  41. files. Add the following lines to the end of the ``CMakeLists.txt`` file:
  42. .. literalinclude:: Step2/CMakeLists.txt
  43. :caption: CMakeLists.txt
  44. :name: CMakeLists.txt-target_include_directories
  45. :language: cmake
  46. :start-after: # so that we will find TutorialConfig.h
  47. Using your favorite editor, create ``TutorialConfig.h.in`` in the source
  48. directory with the following contents:
  49. .. literalinclude:: Step2/TutorialConfig.h.in
  50. :caption: TutorialConfig.h.in
  51. :name: TutorialConfig.h.in
  52. :language: c++
  53. When CMake configures this header file the values for
  54. ``@Tutorial_VERSION_MAJOR@`` and ``@Tutorial_VERSION_MINOR@`` will be
  55. replaced.
  56. Next modify ``tutorial.cxx`` to include the configured header file,
  57. ``TutorialConfig.h``.
  58. Finally, let's print out the executable name and version number by updating
  59. ``tutorial.cxx`` as follows:
  60. .. literalinclude:: Step2/tutorial.cxx
  61. :caption: tutorial.cxx
  62. :name: tutorial.cxx-print-version
  63. :language: c++
  64. :start-after: {
  65. :end-before: // convert input to double
  66. Specify the C++ Standard
  67. -------------------------
  68. Next let's add some C++11 features to our project by replacing ``atof`` with
  69. ``std::stod`` in ``tutorial.cxx``. At the same time, remove
  70. ``#include <cstdlib>``.
  71. .. literalinclude:: Step2/tutorial.cxx
  72. :caption: tutorial.cxx
  73. :name: tutorial.cxx-cxx11
  74. :language: c++
  75. :start-after: // convert input to double
  76. :end-before: // calculate square root
  77. We will need to explicitly state in the CMake code that it should use the
  78. correct flags. The easiest way to enable support for a specific C++ standard
  79. in CMake is by using the :variable:`CMAKE_CXX_STANDARD` variable. For this
  80. tutorial, set the :variable:`CMAKE_CXX_STANDARD` variable in the
  81. ``CMakeLists.txt`` file to ``11`` and :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  82. to ``True``. Make sure to add the ``CMAKE_CXX_STANDARD`` declarations above the
  83. call to ``add_executable``.
  84. .. literalinclude:: Step2/CMakeLists.txt
  85. :caption: CMakeLists.txt
  86. :name: CMakeLists.txt-CXX_STANDARD
  87. :language: cmake
  88. :end-before: # configure a header file to pass some of the CMake settings
  89. Build and Test
  90. --------------
  91. Run the :manual:`cmake <cmake(1)>` executable or the
  92. :manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it
  93. with your chosen build tool.
  94. For example, from the command line we could navigate to the
  95. ``Help/guide/tutorial`` directory of the CMake source code tree and create a
  96. build directory:
  97. .. code-block:: console
  98. mkdir Step1_build
  99. Next, navigate to the build directory and run CMake to configure the project
  100. and generate a native build system:
  101. .. code-block:: console
  102. cd Step1_build
  103. cmake ../Step1
  104. Then call that build system to actually compile/link the project:
  105. .. code-block:: console
  106. cmake --build .
  107. Finally, try to use the newly built ``Tutorial`` with these commands:
  108. .. code-block:: console
  109. Tutorial 4294967296
  110. Tutorial 10
  111. Tutorial