1
0

A Basic Starting Point.rst 4.4 KB

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