@@ -5,44 +5,53 @@ understanding of its fundamentals.
55
66## Index
77
8- * [ 1. Command-line usage] ( #1-command-line-usage )
9- * [ 1.1. Configuration and generation phase] ( #11-configuration-and-generation-phase )
10- * [ 1.2. Build phase] ( #12-build-phase )
11- * [ 2. CMakeLists.txt] ( #2-cmakeliststxt )
12- * [ 2.1. Including other CMake files] ( #21-including-other-cmake-files )
13- * [ 3. CMake syntax] ( #3-cmake-syntax )
14- * [ 3.1. Variables] ( #31-variables )
15- * [ 3.1.1. Setting variables] ( #311-setting-variables )
16- * [ 3.1.2. Working with cache variables] ( #312-working-with-cache-variables )
17- * [ 3.1.3. Using variables] ( #313-using-variables )
18- * [ 3.1.4. Lists] ( #314-lists )
19- * [ 3.2. Functions] ( #32-functions )
20- * [ 3.3. Arguments] ( #33-arguments )
21- * [ 3.3.1. Quoted arguments] ( #331-quoted-arguments )
22- * [ 3.3.2. Unquoted arguments] ( #332-unquoted-arguments )
23- * [ 3.3.3. Bracket arguments] ( #333-bracket-arguments )
24- * [ 4. Targets] ( #4-targets )
25- * [ 4.1. Executables] ( #41-executables )
26- * [ 4.2. OBJECT library] ( #42-object-library )
27- * [ 4.3. SHARED library] ( #43-shared-library )
28- * [ 4.4. MODULE library] ( #44-module-library )
29- * [ 4.5. STATIC library] ( #45-static-library )
30- * [ 4.6. Working with targets] ( #46-working-with-targets )
31- * [ 5. Verification and checks in CMake] ( #5-verification-and-checks-in-cmake )
32- * [ 5.1. Header availability check] ( #51-header-availability-check )
33- * [ 5.2. C source compilation check] ( #52-c-source-compilation-check )
34- * [ 5.3. C source compilation and execution check] ( #53-c-source-compilation-and-execution-check )
35- * [ 6. Generating a configuration header] ( #6-generating-a-configuration-header )
36- * [ 7. Where to go from here?] ( #7-where-to-go-from-here )
37-
38- ## 1. Command-line usage
39-
40- [ CMake] ( https://cmake.org/ ) is an open-source, cross-platform build system
41- generator created by Kitware and contributors. It is invoked from the command
42- line using the ` cmake ` command. When working with CMake, there are two primary
43- phases: the configuration and generation phase, followed by the build phase.
44-
45- ### 1.1. Configuration and generation phase
8+ * [ 1. Introduction] ( #1-introduction )
9+ * [ 2. Command-line usage] ( #2-command-line-usage )
10+ * [ 2.1. Configuration and generation phase] ( #21-configuration-and-generation-phase )
11+ * [ 2.2. Build phase] ( #22-build-phase )
12+ * [ 3. CMakeLists.txt] ( #3-cmakeliststxt )
13+ * [ 3.1. Including other CMake files] ( #31-including-other-cmake-files )
14+ * [ 4. CMake syntax] ( #4-cmake-syntax )
15+ * [ 4.1. Variables] ( #41-variables )
16+ * [ 4.1.1. Setting variables] ( #411-setting-variables )
17+ * [ 4.1.2. Working with cache variables] ( #412-working-with-cache-variables )
18+ * [ 4.1.3. Using variables] ( #413-using-variables )
19+ * [ 4.1.4. Lists] ( #414-lists )
20+ * [ 4.2. Functions] ( #42-functions )
21+ * [ 4.3. Arguments] ( #43-arguments )
22+ * [ 4.3.1. Quoted arguments] ( #431-quoted-arguments )
23+ * [ 4.3.2. Unquoted arguments] ( #432-unquoted-arguments )
24+ * [ 4.3.3. Bracket arguments] ( #433-bracket-arguments )
25+ * [ 5. Targets] ( #5-targets )
26+ * [ 5.1. Executables] ( #51-executables )
27+ * [ 5.2. OBJECT library] ( #52-object-library )
28+ * [ 5.3. SHARED library] ( #53-shared-library )
29+ * [ 5.4. MODULE library] ( #54-module-library )
30+ * [ 5.5. STATIC library] ( #55-static-library )
31+ * [ 5.6. Working with targets] ( #56-working-with-targets )
32+ * [ 6. Verification and checks in CMake] ( #6-verification-and-checks-in-cmake )
33+ * [ 6.1. Header availability check] ( #61-header-availability-check )
34+ * [ 6.2. C source compilation check] ( #62-c-source-compilation-check )
35+ * [ 6.3. C source compilation and execution check] ( #63-c-source-compilation-and-execution-check )
36+ * [ 7. Generating a configuration header] ( #7-generating-a-configuration-header )
37+ * [ 8. Where to go from here?] ( #8-where-to-go-from-here )
38+
39+ ## 1. Introduction
40+
41+ [ CMake] ( https://cmake.org/ ) is an open-source, cross-platform meta build system
42+ created by Kitware and contributors. It's not a build system * per se* , but
43+ rather a build system generator that produces configuration files for specific
44+ build systems, such as Unix Makefiles, Visual Studio projects, or Ninja build
45+ files.
46+
47+ ## 2. Command-line usage
48+
49+ CMake is typically invoked from the command line using the ` cmake ` command. When
50+ working with CMake, there are two primary phases: the configuration and
51+ generation phase, where CMake sets up the project's build files, and the build
52+ phase, where the target build system compiles the project.
53+
54+ ### 2.1. Configuration and generation phase
4655
4756In this phase, CMake performs essential tasks to set up a build environment. It
4857reads source files (` CMakeLists.txt ` ) from the source directory, configures the
@@ -54,7 +63,7 @@ into a build directory.
5463cmake -S source-directory -B build-directory
5564```
5665
57- ### 1 .2. Build phase
66+ ### 2 .2. Build phase
5867
5968The build phase involves transforming project C/C++ source files into libraries
6069and executables. During this phase, the project undergoes compilation and
@@ -85,7 +94,7 @@ cmake --build build-directory --parallel
8594> cmake --build build-directory --parallel
8695> ` ` `
8796
88- # # 2 . CMakeLists.txt
97+ # # 3 . CMakeLists.txt
8998
9099In the world of CMake, the ` CMakeLists.txt` files serve as blueprints for
91100configuring and building projects. These files define how the project source
@@ -118,7 +127,7 @@ Project source directory example:
118127└─📄 ...
119128```
120129
121- ### 2 .1. Including other CMake files
130+ ### 3 .1. Including other CMake files
122131
123132To maintain modularity and organization, other CMake files can be included
124133within the project:
@@ -136,16 +145,16 @@ add_subdirectory(subdirectory)
136145
137146This allows breaking down complex configurations into manageable components.
138147
139- ## 3 . CMake syntax
148+ ## 4 . CMake syntax
140149
141- ### 3 .1. Variables
150+ ### 4 .1. Variables
142151
143152In CMake, variables are essential for storing and manipulating data throughout
144153the project's configuration and build processes. They play a pivotal role in
145154customizing builds and managing project-specific settings. Variable names are
146155case-sensitive.
147156
148- #### 3 .1.1. Setting variables
157+ #### 4 .1.1. Setting variables
149158
150159Variables are set using the ` set() ` command, where a value is assigned to a
151160variable:
@@ -163,7 +172,7 @@ store values that remain consistent across different CMake runs and are
163172accessible to various parts of the project. These variables also require a
164173short documentation help text to describe their purpose.
165174
166- #### 3 .1.2. Working with cache variables
175+ #### 4 .1.2. Working with cache variables
167176
168177Cache variables are highly versatile and can be influenced from various sources,
169178such as the command line. This allows for dynamic configuration adjustments:
@@ -176,7 +185,7 @@ cmake -DFOOBAR="value" -S source-directory -B build-directory
176185Cache variables become particularly useful for customizing builds, specifying
177186project-wide settings, and adapting configurations to different environments.
178187
179- #### 3 .1.3. Using variables
188+ #### 4 .1.3. Using variables
180189
181190Variable references in CMake use ` $ ` sigil symbol and are enclosed within curly
182191brackets ` {} ` .
@@ -198,7 +207,7 @@ endif()
198207# Output: Variable foobar=value
199208```
200209
201- #### 3 .1.4. Lists
210+ #### 4 .1.4. Lists
202211
203212Lists in CMake are strings separated with ` ; ` that can be iterated over in
204213loops, such as ` foreach ` .
@@ -219,7 +228,7 @@ The `list()` command performs operations on lists.
219228Lists are frequently used for tasks like specifying source files, compiler
220229flags, and dependencies.
221230
222- ### 3 .2. Functions
231+ ### 4 .2. Functions
223232
224233CMake function is created with the ` function() ` command:
225234
@@ -235,27 +244,27 @@ print_message("Hello, World")
235244# Output: Hello, World
236245```
237246
238- ### 3 .3. Arguments
247+ ### 4 .3. Arguments
239248
240249Arguments in CMake can be passed to commands in three ways.
241250
242- #### 3 .3.1. Quoted arguments
251+ #### 4 .3.1. Quoted arguments
243252
244253Here variable is set to a literal string ` quoted argument ` :
245254
246255``` cmake
247256set(foobar "quoted argument")
248257```
249258
250- #### 3 .3.2. Unquoted arguments
259+ #### 4 .3.2. Unquoted arguments
251260
252261Here variable is set to a literal string ` unquoted ` :
253262
254263``` cmake
255264set(foobar unquoted)
256265```
257266
258- #### 3 .3.3. Bracket arguments
267+ #### 4 .3.3. Bracket arguments
259268
260269Bracket arguments are wrapped in pairs of double brackets ` [[..]] ` and any
261270number of ` = ` characters in between (` [[ ` , ` ]] ` , ` [=[ ` , ` ]=] ` , ` [==[ ` , ` ]==] ` ,
@@ -270,7 +279,7 @@ not evaluated. Argument can also contain ; and other special ]] characters.
270279]=])
271280```
272281
273- ## 4 . Targets
282+ ## 5 . Targets
274283
275284CMake revolves around targets, which represent various components of the
276285project. There are primarily two types: libraries and executables.
@@ -310,7 +319,7 @@ add_library(extension SHARED extension.c src.c)
310319The concepts of executable and library targets can be illustrated through
311320examples of using a compiler like `gcc`.
312321
313- ### 4 .1. Executables
322+ ### 5 .1. Executables
314323
315324Executables are programs that are intended to be run.
316325
@@ -321,7 +330,7 @@ gcc -o php php.c
321330./php
322331```
323332
324- ### 4 .2. OBJECT library
333+ ### 5 .2. OBJECT library
325334
326335When using OBJECT library, each source file will be compiled to a binary object
327336file. Behind the scene, CMake takes care of compile flags and adjusts the build
@@ -333,7 +342,7 @@ gcc -c -o extension.o extension.c
333342gcc -c -o src.o src.c
334343```
335344
336- ### 4 .3. SHARED library
345+ ### 5 .3. SHARED library
337346
338347CMake automatically adds sensible linker flags when building ` SHARED ` library.
339348For example, ` -shared ` , ` -Wl,-soname,extension.so ` , position-independent code
@@ -347,7 +356,7 @@ gcc -fPIC -c -o src.o src.c
347356gcc -fPIC -shared -Wl,-soname,extension.so -o extension.so extension.o src.o
348357```
349358
350- ### 4 .4. MODULE library
359+ ### 5 .4. MODULE library
351360
352361The ` MODULE ` library, on the other hand, is similar to the ` SHARED ` . However,
353362CMake uses slightly different flags and treats it differently in CMake code. A
@@ -380,7 +389,7 @@ int main(void)
380389}
381390```
382391
383- ### 4 .5. STATIC library
392+ ### 5 .5. STATIC library
384393
385394`STATIC` libraries are intended to be linked statically to other libraries or
386395executables where they become part of the final binary.
@@ -394,7 +403,7 @@ ar rcs libmain.a main.o
394403gcc -o program program.c -L. -lmain
395404```
396405
397- ### 4 .6. Working with targets
406+ ### 5 .6. Working with targets
398407
399408Once targets are defined, they can be fine-tuned with additional configurations:
400409
@@ -420,7 +429,7 @@ the item is accessible both to the defined target and any depending targets.
420429Lastly, ` INTERFACE ` denotes that the item is solely accessible to depending
421430targets and is not accessible to the defining target itself.
422431
423- ## 5 . Verification and checks in CMake
432+ ## 6 . Verification and checks in CMake
424433
425434In CMake, various verification and validation tasks can be performed to ensure
426435the availability of headers, symbols, struct members, as well as assess the
@@ -431,7 +440,7 @@ CMake provides a range of commands, many of which are found in separate CMake
431440modules bundled with CMake. These modules need to be included before utilizing
432441the respective verification commands:
433442
434- ### 5 .1. Header availability check
443+ ### 6 .1. Header availability check
435444
436445To verify if a header file is available:
437446
@@ -440,7 +449,7 @@ include(CheckIncludeFile)
440449check_include_file(sys/types.h HAVE_SYS_TYPES_H)
441450```
442451
443- ### 5 .2. C source compilation check
452+ ### 6 .2. C source compilation check
444453
445454To determine if a C source file compiles and links into an executable:
446455
@@ -455,7 +464,7 @@ This command initiates a compilation and linking step, as illustrated here:
455464gcc -o out check_program.c
456465```
457466
458- ### 5 .3. C source compilation and execution check
467+ ### 6 .3. C source compilation and execution check
459468
460469For a more comprehensive assessment that includes compiling, linking, and
461470executing the C code:
@@ -473,7 +482,7 @@ gcc -o out check_program.c
473482./out
474483```
475484
476- ## 6 . Generating a configuration header
485+ ## 7 . Generating a configuration header
477486
478487Once the necessary checks have been completed during the configuration phase,
479488a configuration header file can be created. This header file serves as a
@@ -528,7 +537,7 @@ int main(void)
528537}
529538```
530539
531- ## 7 . Where to go from here?
540+ ## 8 . Where to go from here?
532541
533542This section has provided a general overview of the most crucial features of
534543CMake. To explore deeper into mastering CMake, it is highly recommended to start
0 commit comments