You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/basics.md
+52-10Lines changed: 52 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,23 +16,36 @@ A CMake **project** is a collection of targets. In the context of libraries the
16
16
17
17
## Targets
18
18
19
-
The basic unit of CMake is called a **target**. A target (also referred to as [binary target](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#binary-targets) in the CMake documentation) corresponds to an executable or library you can build. There are also [pseudo targets](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#pseudo-targets), but we ignore them for now.
19
+
The basic unit of CMake is called a **target**. A target (also referred to as [binary target](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#binary-targets) in the CMake documentation) corresponds to an executable or static/dynamic library you can build. There are also [pseudo targets](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#pseudo-targets), but we ignore them for now.
20
20
21
21
<sub>Visual Studio: a **target** corresponds to a _project_.</sub>
22
22
23
+
Target types:
24
+
25
+
|Type|Purpose|
26
+
|-|-|
27
+
|Executable|Program you run on your computer (`.exe`)|
28
+
|Static library|Library code _compiled into_ the final executable (`.lib`, `.a`)|
29
+
|Dynamic library|Library code loaded at runtime (`.dll`, `.so`, `.dylib`)|
30
+
|Interface|Used for organizational purposes (common flags/includes/libraries)|
31
+
23
32
## Target Properties
24
33
25
34
Targets have a collection of **properties** that describe how to build them.
26
35
27
-
Examples of properties:
36
+
The most commonly-used properties:
28
37
29
-
-_Sources_: the `*.cpp` files used to build the target.
30
-
-_Compile options_: Command line flags for the compiler.
31
-
-_Link libraries_: The **dependencies** required to build this target.
38
+
-[_Sources_](https://cmake.org/cmake/help/latest/command/target_sources.html): the `*.cpp` files used to build the target.
39
+
-[_Include directories_](https://cmake.org/cmake/help/latest/command/target_include_directories.html): Paths to directories the compiler will search when doing `#include`.
40
+
-[_Compile definitions_](https://cmake.org/cmake/help/latest/command/target_compile_definitions.html): Preprocessor macro definitions used for compilation (`-DMYOPTION`).
41
+
-[_Compile options_](https://cmake.org/cmake/help/latest/command/target_compile_options.html): Command line flags for the compiler (platform/compiler specific).
42
+
-[_Compile features_](https://cmake.org/cmake/help/latest/command/target_compile_features.html): C/C++ standard version to build with.
43
+
-[_Link libraries_](https://cmake.org/cmake/help/latest/command/target_link_libraries.html): The **dependencies** required to build this target.
44
+
-[_Link options_](https://cmake.org/cmake/help/latest/command/target_link_options.html): Command line flags for the linker (platform/linker specific).
32
45
33
46
<sub>See the [CMake documentation](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets) for an exhaustive list of target properties.</sub>
34
47
35
-
**Important**: The term **link** has a slightly different meaning in CMake than you might expect. In addition to adding a library to the command line of the linker, CMake also propagates properties of the target you link to.
48
+
**Important**: The term **link** has a slightly different meaning in CMake than you might expect. In addition to adding a library to the command line of the linker, CMake also (transitively) propagates properties of the target you link to based on their _visibility_.
36
49
37
50
<sub>You can think of **linking** as _depending on_.</sub>
38
51
@@ -44,7 +57,7 @@ The propagation of properties depends on their **visibility**:
44
57
45
58
In practice you default to **private**, unless consumers of your library _require_ the property to build their target. In that case you use **public**.
46
59
47
-
###Example
60
+
## Example
48
61
49
62
The most intuitive example is with _include directories_. Imagine there are two targets:
50
63
@@ -67,10 +80,39 @@ name = "DataProcessor"
67
80
type = "static"
68
81
sources = ["StringUtils/src/stringutils.cpp"]
69
82
headers = ["StringUtils/include/stringutils.hpp"]
70
-
include-directories = ["StringUtils/include"]
83
+
include-directories = ["StringUtils/include"]# public (default for libraries)
71
84
72
85
[target.DataProcessor]
73
86
type = "executable"
74
87
sources = ["DataProcessor/src/main.cpp"]
75
-
link-libraries = ["StringUtils"]
76
-
```
88
+
link-libraries = ["StringUtils"] # private (default for executables)
89
+
```
90
+
91
+
The generated `CMakeLists.txt` (simplified) looks like this:
0 commit comments