A Basic Starting Point.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. Any project's top most ``CMakeLists.txt`` must start by specifying
  16. a minimum CMake version using :command:`cmake_minimum_required`. This ensures
  17. that the later CMake functions are run with a compatible version of CMake.
  18. To start a project, we use :command:`project` to set the project name. This
  19. call is required with every project and should be called soon after
  20. :command:`cmake_minimum_required`.
  21. Lastly, we use :command:`add_executable` to specify we want an executable
  22. named Tutorial generated using ``tutorial.cxx`` as the source.
  23. Note that this example uses lower case commands in the ``CMakeLists.txt``
  24. file. Upper, lower, and mixed case commands are supported by CMake. The source
  25. code for ``tutorial.cxx`` is provided in the ``Step1`` directory and can be
  26. used to compute the square root of a number.
  27. Build and Run
  28. -------------
  29. That's all that is needed - we can build and run our project now! First, run
  30. the :manual:`cmake <cmake(1)>` executable or the
  31. :manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it
  32. with your chosen build tool.
  33. For example, from the command line we could navigate to the
  34. ``Help/guide/tutorial`` directory of the CMake source code tree and create a
  35. build directory:
  36. .. code-block:: console
  37. mkdir Step1_build
  38. Next, navigate to the build directory and run CMake to configure the project
  39. and generate a native build system:
  40. .. code-block:: console
  41. cd Step1_build
  42. cmake ../Step1
  43. Then call that build system to actually compile/link the project:
  44. .. code-block:: console
  45. cmake --build .
  46. Finally, try to use the newly built ``Tutorial`` with these commands:
  47. .. code-block:: console
  48. Tutorial 4294967296
  49. Tutorial 10
  50. Tutorial
  51. Adding a Version Number and Configured Header File
  52. --------------------------------------------------
  53. The first feature we will add is to provide our executable and project with a
  54. version number. While we could do this exclusively in the source code, using
  55. ``CMakeLists.txt`` provides more flexibility.
  56. First, modify the ``CMakeLists.txt`` file to use the :command:`project` command
  57. to set the project name and version number.
  58. .. literalinclude:: Step2/CMakeLists.txt
  59. :caption: CMakeLists.txt
  60. :name: CMakeLists.txt-project-VERSION
  61. :language: cmake
  62. :end-before: # specify the C++ standard
  63. Then use :command:`configure_file` to pass the version number to the source
  64. code:
  65. .. literalinclude:: Step2/CMakeLists.txt
  66. :caption: CMakeLists.txt
  67. :name: CMakeLists.txt-configure_file
  68. :language: cmake
  69. :start-after: # to the source code
  70. :end-before: # add the executable
  71. Since the configured file will be written into the binary tree, we
  72. must add that directory to the list of paths to search for include
  73. files. Use :command:`target_include_directories` to add the following lines to
  74. the end of the ``CMakeLists.txt`` file:
  75. .. literalinclude:: Step2/CMakeLists.txt
  76. :caption: CMakeLists.txt
  77. :name: CMakeLists.txt-target_include_directories
  78. :language: cmake
  79. :start-after: # so that we will find TutorialConfig.h
  80. Using your favorite editor, create ``TutorialConfig.h.in`` in the source
  81. directory with the following contents:
  82. .. literalinclude:: Step2/TutorialConfig.h.in
  83. :caption: TutorialConfig.h.in
  84. :name: TutorialConfig.h.in
  85. :language: c++
  86. When CMake configures this header file, the values for
  87. ``@Tutorial_VERSION_MAJOR@`` and ``@Tutorial_VERSION_MINOR@`` will be
  88. replaced with the corresponding version numbers from the project.
  89. Next modify ``tutorial.cxx`` to include the configured header file,
  90. ``TutorialConfig.h``.
  91. Finally, let's print out the executable name and version number by updating
  92. ``tutorial.cxx`` as follows:
  93. .. literalinclude:: Step2/tutorial.cxx
  94. :caption: tutorial.cxx
  95. :name: tutorial.cxx-print-version
  96. :language: c++
  97. :start-after: {
  98. :end-before: // convert input to double
  99. Specify the C++ Standard
  100. -------------------------
  101. Next let's add some C++11 features to our project by replacing ``atof`` with
  102. ``std::stod`` in ``tutorial.cxx``. At the same time, remove
  103. ``#include <cstdlib>``.
  104. .. literalinclude:: Step2/tutorial.cxx
  105. :caption: tutorial.cxx
  106. :name: tutorial.cxx-cxx11
  107. :language: c++
  108. :start-after: // convert input to double
  109. :end-before: // calculate square root
  110. We will need to explicitly state in the CMake code that it should use the
  111. correct flags. The easiest way to enable support for a specific C++ standard
  112. in CMake is by using the :variable:`CMAKE_CXX_STANDARD` variable. For this
  113. tutorial, :command:`set` the :variable:`CMAKE_CXX_STANDARD` variable in the
  114. ``CMakeLists.txt`` file to ``11`` and :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  115. to ``True``. Make sure to add the ``CMAKE_CXX_STANDARD`` declarations above the
  116. call to ``add_executable``.
  117. .. literalinclude:: Step2/CMakeLists.txt
  118. :caption: CMakeLists.txt
  119. :name: CMakeLists.txt-CXX_STANDARD
  120. :language: cmake
  121. :end-before: # configure a header file to pass some of the CMake settings
  122. Rebuild
  123. -------
  124. Let's build our project again. We already created a build directory and ran
  125. CMake, so we can skip to the build step:
  126. .. code-block:: console
  127. cd Step1_build
  128. cmake --build .
  129. Now we can try to use the newly built ``Tutorial`` with same commands as before:
  130. .. code-block:: console
  131. Tutorial 4294967296
  132. Tutorial 10
  133. Tutorial
  134. Check that the version number is now reported when running the executable without
  135. any arguments.