A Basic Starting Point.rst 4.2 KB

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