A Basic Starting Point.rst 5.2 KB

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