Skip to content

Commit 76b7b8e

Browse files
authored
Clarify basics documentation page
1 parent 501b26c commit 76b7b8e

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

docs/basics.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,36 @@ A CMake **project** is a collection of targets. In the context of libraries the
1616

1717
## Targets
1818

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.
2020

2121
<sub>Visual Studio: a **target** corresponds to a _project_.</sub>
2222

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+
2332
## Target Properties
2433

2534
Targets have a collection of **properties** that describe how to build them.
2635

27-
Examples of properties:
36+
The most commonly-used properties:
2837

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).
3245

3346
<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>
3447

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_.
3649

3750
<sub>You can think of **linking** as _depending on_.</sub>
3851

@@ -44,7 +57,7 @@ The propagation of properties depends on their **visibility**:
4457

4558
In practice you default to **private**, unless consumers of your library _require_ the property to build their target. In that case you use **public**.
4659

47-
### Example
60+
## Example
4861

4962
The most intuitive example is with _include directories_. Imagine there are two targets:
5063

@@ -67,10 +80,39 @@ name = "DataProcessor"
6780
type = "static"
6881
sources = ["StringUtils/src/stringutils.cpp"]
6982
headers = ["StringUtils/include/stringutils.hpp"]
70-
include-directories = ["StringUtils/include"]
83+
include-directories = ["StringUtils/include"] # public (default for libraries)
7184

7285
[target.DataProcessor]
7386
type = "executable"
7487
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:
92+
93+
```cmake
94+
project(DataProcessor)
95+
96+
# Target: StringUtils
97+
add_library(StringUtils STATIC
98+
"StringUtils/include/stringutils.hpp"
99+
"StringUtils/src/stringutils.cpp"
100+
)
101+
102+
target_sources(StringUtils PRIVATE ${StringUtils_SOURCES})
103+
104+
target_include_directories(StringUtils PUBLIC
105+
"StringUtils/include"
106+
)
107+
108+
# Target: DataProcessor
109+
add_executable(DataProcessor
110+
"DataProcessor/src/main.cpp"
111+
)
112+
113+
target_sources(DataProcessor PRIVATE ${DataProcessor_SOURCES})
114+
115+
target_link_libraries(DataProcessor PRIVATE
116+
StringUtils
117+
)
118+
```

0 commit comments

Comments
 (0)