A Basic Starting Point.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. Step 1: A Basic Starting Point
  2. ==============================
  3. Where do I start with CMake? This step will provide an introduction to some of
  4. CMake's basic syntax, commands, and variables. As these concepts are
  5. introduced, we will work through three exercises and create a simple CMake
  6. project.
  7. Each exercise in this step will start with some background information. Then, a
  8. goal and list of helpful resources are provided. Each file in the
  9. ``Files to Edit`` section is in the ``Step1`` directory and contains one or
  10. more ``TODO`` comments. Each ``TODO`` represents a line or two of code to
  11. change or add. The ``TODO`` s are intended to be completed in numerical order,
  12. first complete ``TODO 1`` then ``TODO 2``, etc. The ``Getting Started``
  13. section will give some helpful hints and guide you through the exercise. Then
  14. the ``Build and Run`` section will walk step-by-step through how to build and
  15. test the exercise. Finally, at the end of each exercise the intended solution
  16. is discussed.
  17. Also note that each step in the tutorial builds on the next. So, for example,
  18. the starting code for ``Step2`` is the complete solution to ``Step1``.
  19. Exercise 1 - Building a Basic Project
  20. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  21. The most basic CMake project is an executable built from a single source code
  22. file. For simple projects like this, a ``CMakeLists.txt`` file with three
  23. commands is all that is required.
  24. **Note:** Although upper, lower and mixed case commands are supported by CMake,
  25. lower case commands are preferred and will be used throughout the tutorial.
  26. Any project's top most CMakeLists.txt must start by specifying a minimum CMake
  27. version using the :command:`cmake_minimum_required` command. This establishes
  28. policy settings and ensures that the following CMake functions are run with a
  29. compatible version of CMake.
  30. To start a project, we use the :command:`project` command to set the project
  31. name. This call is required with every project and should be called soon after
  32. :command:`cmake_minimum_required`. As we will see later, this command can
  33. also be used to specify other project level information such as the language
  34. or version number.
  35. Finally, the :command:`add_executable` command tells CMake to create an
  36. executable using the specified source code files.
  37. Goal
  38. ----
  39. Understand how to create a simple CMake project.
  40. Helpful Resources
  41. -----------------
  42. * :command:`add_executable`
  43. * :command:`cmake_minimum_required`
  44. * :command:`project`
  45. Files to Edit
  46. -------------
  47. * ``CMakeLists.txt``
  48. Getting Started
  49. ----------------
  50. The source code for ``tutorial.cxx`` is provided in the
  51. ``Help/guide/tutorial/Step1`` directory and can be used to compute the square
  52. root of a number. This file does not need to be edited in this step.
  53. In the same directory is a ``CMakeLists.txt`` file which you will complete.
  54. Start with ``TODO 1`` and work through ``TODO 3``.
  55. Build and Run
  56. -------------
  57. Once ``TODO 1`` through ``TODO 3`` have been completed, we are ready to build
  58. and run our project! First, run the :manual:`cmake <cmake(1)>` executable or the
  59. :manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it
  60. with your chosen build tool.
  61. For example, from the command line we could navigate to the
  62. ``Help/guide/tutorial`` directory of the CMake source code tree and create a
  63. build directory:
  64. .. code-block:: console
  65. mkdir Step1_build
  66. Next, navigate to that build directory and run
  67. :manual:`cmake <cmake(1)>` to configure the project and generate a native build
  68. system:
  69. .. code-block:: console
  70. cd Step1_build
  71. cmake ../Step1
  72. Then call that build system to actually compile/link the project:
  73. .. code-block:: console
  74. cmake --build .
  75. For multi-config generators (e.g. Visual Studio), first navigate to the
  76. appropriate subdirectory, for example:
  77. .. code-block:: console
  78. cd Debug
  79. Finally, try to use the newly built ``Tutorial``:
  80. .. code-block:: console
  81. Tutorial 4294967296
  82. Tutorial 10
  83. Tutorial
  84. **Note:** Depending on the shell, the correct syntax may be ``Tutorial``,
  85. ``./Tutorial`` or ``.\Tutorial``. For simplicity, the exercises will use
  86. ``Tutorial`` throughout.
  87. Solution
  88. --------
  89. As mentioned above, a three line ``CMakeLists.txt`` is all that we need to get
  90. up and running. The first line is to use :command:`cmake_minimum_required` to
  91. set the CMake version as follows:
  92. .. raw:: html
  93. <details><summary>TODO 1: Click to show/hide answer</summary>
  94. .. literalinclude:: Step2/CMakeLists.txt
  95. :caption: TODO 1: CMakeLists.txt
  96. :name: CMakeLists.txt-cmake_minimum_required
  97. :language: cmake
  98. :end-before: # set the project name and version
  99. .. raw:: html
  100. </details>
  101. The next step to make a basic project is to use the :command:`project`
  102. command as follows to set the project name:
  103. .. raw:: html
  104. <details><summary>TODO 2: Click to show/hide answer</summary>
  105. .. code-block:: cmake
  106. :caption: TODO 2: CMakeLists.txt
  107. :name: CMakeLists.txt-project
  108. project(Tutorial)
  109. .. raw:: html
  110. </details>
  111. The last command to call for a basic project is
  112. :command:`add_executable`. We call it as follows:
  113. .. raw:: html
  114. <details><summary>TODO 3: Click to show/hide answer</summary>
  115. .. literalinclude:: Step2/CMakeLists.txt
  116. :caption: TODO 3: CMakeLists.txt
  117. :name: CMakeLists.txt-add_executable
  118. :language: cmake
  119. :start-after: # add the executable
  120. :end-before: # TODO 3:
  121. .. raw:: html
  122. </details>
  123. Exercise 2 - Specifying the C++ Standard
  124. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  125. CMake has some special variables that are either created behind the scenes or
  126. have meaning to CMake when set by project code. Many of these variables start
  127. with ``CMAKE_``. Avoid this naming convention when creating variables for your
  128. projects. Two of these special user settable variables are
  129. :variable:`CMAKE_CXX_STANDARD` and :variable:`CMAKE_CXX_STANDARD_REQUIRED`.
  130. These may be used together to specify the C++ standard needed to build the
  131. project.
  132. Goal
  133. ----
  134. Add a feature that requires C++11.
  135. Helpful Resources
  136. -----------------
  137. * :variable:`CMAKE_CXX_STANDARD`
  138. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  139. * :command:`set`
  140. Files to Edit
  141. -------------
  142. * ``CMakeLists.txt``
  143. * ``tutorial.cxx``
  144. Getting Started
  145. ---------------
  146. Continue editing files in the ``Step1`` directory. Start with ``TODO 4`` and
  147. complete through ``TODO 6``.
  148. First, edit ``tutorial.cxx`` by adding a feature that requires C++11. Then
  149. update ``CMakeLists.txt`` to require C++11.
  150. Build and Run
  151. -------------
  152. Let's build our project again. Since we already created a build directory and
  153. ran CMake for Exercise 1, we can skip to the build step:
  154. .. code-block:: console
  155. cd Step1_build
  156. cmake --build .
  157. Now we can try to use the newly built ``Tutorial`` with same commands as
  158. before:
  159. .. code-block:: console
  160. Tutorial 4294967296
  161. Tutorial 10
  162. Tutorial
  163. Solution
  164. --------
  165. We start by adding some C++11 features to our project by replacing
  166. ``atof`` with ``std::stod`` in ``tutorial.cxx``. This looks like
  167. the following:
  168. .. raw:: html
  169. <details><summary>TODO 4: Click to show/hide answer</summary>
  170. .. literalinclude:: Step2/tutorial.cxx
  171. :caption: TODO 4: tutorial.cxx
  172. :name: tutorial.cxx-cxx11
  173. :language: c++
  174. :start-after: // convert input to double
  175. :end-before: // TODO 6:
  176. .. raw:: html
  177. </details>
  178. To complete ``TODO 5``, simply remove ``#include <cstdlib>``.
  179. We will need to explicitly state in the CMake code that it should use the
  180. correct flags. One way to enable support for a specific C++ standard in CMake
  181. is by using the :variable:`CMAKE_CXX_STANDARD` variable. For this tutorial, set
  182. the :variable:`CMAKE_CXX_STANDARD` variable in the ``CMakeLists.txt`` file to
  183. ``11`` and :variable:`CMAKE_CXX_STANDARD_REQUIRED` to ``True``. Make sure to
  184. add the :variable:`CMAKE_CXX_STANDARD` declarations above the call to
  185. :command:`add_executable`.
  186. .. raw:: html
  187. <details><summary>TODO 6: Click to show/hide answer</summary>
  188. .. literalinclude:: Step2/CMakeLists.txt
  189. :caption: TODO 6: CMakeLists.txt
  190. :name: CMakeLists.txt-CXX_STANDARD
  191. :language: cmake
  192. :start-after: # specify the C++ standard
  193. :end-before: # configure a header file
  194. .. raw:: html
  195. </details>
  196. Exercise 3 - Adding a Version Number and Configured Header File
  197. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  198. Sometimes it may be useful to have a variable that is defined in your
  199. ``CMakelists.txt`` file also be available in your source code. In this case, we
  200. would like to print the project version.
  201. One way to accomplish this is by using a configured header file. We create an
  202. input file with one or more variables to replace. These variables have special
  203. syntax which looks like ``@VAR@``.
  204. Then, we use the :command:`configure_file` command to copy the input file to a
  205. given output file and replace these variables with the current value of ``VAR``
  206. in the ``CMakelists.txt`` file.
  207. While we could edit the version directly in the source code, using this
  208. feature is preferred since it creates a single source of truth and avoids
  209. duplication.
  210. Goal
  211. ----
  212. Define and report the project's version number.
  213. Helpful Resources
  214. -----------------
  215. * :variable:`<PROJECT-NAME>_VERSION_MAJOR`
  216. * :variable:`<PROJECT-NAME>_VERSION_MINOR`
  217. * :command:`configure_file`
  218. * :command:`target_include_directories`
  219. Files to Edit
  220. -------------
  221. * ``CMakeLists.txt``
  222. * ``tutorial.cxx``
  223. Getting Started
  224. ---------------
  225. Continue to edit files from ``Step1``. Start on ``TODO 7`` and complete through
  226. ``TODO 12``. In this exercise, we start by adding a project version number in
  227. ``CMakeLists.txt``. In that same file, use :command:`configure_file` to copy a
  228. given input file to an output file and substitute some variable values in the
  229. input file content.
  230. Next, create an input header file ``TutorialConfig.h.in`` defining version
  231. numbers which will accept variables passed from :command:`configure_file`.
  232. Finally, update ``tutorial.cxx`` to print out its version number.
  233. Build and Run
  234. -------------
  235. Let's build our project again. As before, we already created a build directory
  236. and ran CMake so we can skip to the build step:
  237. .. code-block:: console
  238. cd Step1_build
  239. cmake --build .
  240. Verify that the version number is now reported when running the executable
  241. without any arguments.
  242. Solution
  243. --------
  244. In this exercise, we improve our executable by printing a version number.
  245. While we could do this exclusively in the source code, using ``CMakeLists.txt``
  246. lets us maintain a single source of data for the version number.
  247. First, we modify the ``CMakeLists.txt`` file to use the
  248. :command:`project` command to set both the project name and version number.
  249. When the :command:`project` command is called, CMake defines
  250. ``Tutorial_VERSION_MAJOR`` and ``Tutorial_VERSION_MINOR`` behind the scenes.
  251. .. raw:: html
  252. <details><summary>TODO 7: Click to show/hide answer</summary>
  253. .. literalinclude:: Step2/CMakeLists.txt
  254. :caption: TODO 7: CMakeLists.txt
  255. :name: CMakeLists.txt-project-VERSION
  256. :language: cmake
  257. :start-after: # set the project name and version
  258. :end-before: # specify the C++ standard
  259. .. raw:: html
  260. </details>
  261. Then we used :command:`configure_file` to copy the input file with the
  262. specified CMake variables replaced:
  263. .. raw:: html
  264. <details><summary>TODO 8: Click to show/hide answer</summary>
  265. .. literalinclude:: Step2/CMakeLists.txt
  266. :caption: TODO 8: CMakeLists.txt
  267. :name: CMakeLists.txt-configure_file
  268. :language: cmake
  269. :start-after: # to the source code
  270. :end-before: # TODO 2:
  271. .. raw:: html
  272. </details>
  273. Since the configured file will be written into the project binary
  274. directory, we must add that directory to the list of paths to search for
  275. include files.
  276. **Note:** Throughout this tutorial, we will refer to the project build and
  277. the project binary directory interchangeably. These are the same and are not
  278. meant to refer to a `bin/` directory.
  279. We used :command:`target_include_directories` to specify
  280. where the executable target should look for include files.
  281. .. raw:: html
  282. <details><summary>TODO 9: Click to show/hide answer</summary>
  283. .. literalinclude:: Step2/CMakeLists.txt
  284. :caption: TODO 9: CMakeLists.txt
  285. :name: CMakeLists.txt-target_include_directories
  286. :language: cmake
  287. :start-after: # so that we will find TutorialConfig.h
  288. .. raw:: html
  289. </details>
  290. ``TutorialConfig.h.in`` is the input header file to be configured.
  291. When :command:`configure_file` is called from our ``CMakeLists.txt``, the
  292. values for ``@Tutorial_VERSION_MAJOR@`` and ``@Tutorial_VERSION_MINOR@`` will
  293. be replaced with the corresponding version numbers from the project in
  294. ``TutorialConfig.h``.
  295. .. raw:: html
  296. <details><summary>TODO 10: Click to show/hide answer</summary>
  297. .. literalinclude:: Step2/TutorialConfig.h.in
  298. :caption: TODO 10: TutorialConfig.h.in
  299. :name: TutorialConfig.h.in
  300. :language: c++
  301. .. raw:: html
  302. </details>
  303. Next, we need to modify ``tutorial.cxx`` to include the configured header file,
  304. ``TutorialConfig.h``.
  305. .. raw:: html
  306. <details><summary>TODO 11: Click to show/hide answer</summary>
  307. .. code-block:: c++
  308. :caption: TODO 11: tutorial.cxx
  309. #include "TutorialConfig.h"
  310. .. raw:: html
  311. </details>
  312. Finally, we print out the executable name and version number by updating
  313. ``tutorial.cxx`` as follows:
  314. .. raw:: html
  315. <details><summary>TODO 12: Click to show/hide answer</summary>
  316. .. literalinclude:: Step2/tutorial.cxx
  317. :caption: TODO 12 : tutorial.cxx
  318. :name: tutorial.cxx-print-version
  319. :language: c++
  320. :start-after: {
  321. :end-before: // convert input to double
  322. .. raw:: html
  323. </details>