Skip to content

Commit efa4f71

Browse files
authored
Add CMake phases to basics
1 parent 76b7b8e commit efa4f71

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

docs/basics.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ nav_order: 1
66

77
# Basics
88

9-
To effectively use cmkr it helps to understand the basic concepts of CMake.
9+
To effectively use cmkr it helps to understand the basic concepts of CMake. This page serves as an introduction to CMake for beginners. Links to the CMake documentation are included for reference.
1010

1111
## Projects
1212

@@ -16,11 +16,11 @@ 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 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.
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.
2020

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

23-
Target types:
23+
Most important target types:
2424

2525
|Type|Purpose|
2626
|-|-|
@@ -29,9 +29,11 @@ Target types:
2929
|Dynamic library|Library code loaded at runtime (`.dll`, `.so`, `.dylib`)|
3030
|Interface|Used for organizational purposes (common flags/includes/libraries)|
3131

32+
There are also other [pseudo targets](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#pseudo-targets), but they are not important for now.
33+
3234
## Target Properties
3335

34-
Targets have a collection of **properties** that describe how to build them.
36+
Targets have a collection of **properties** that describe how the compiler builds them.
3537

3638
The most commonly-used properties:
3739

@@ -45,9 +47,9 @@ The most commonly-used properties:
4547

4648
<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>
4749

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_.
50+
**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, CMake also (transitively) propagates properties of the target you link to based on their _visibility_.
4951

50-
<sub>You can think of **linking** as _depending on_.</sub>
52+
You should think of **linking** in CMake as _depending on_.{:.info}
5153

5254
The propagation of properties depends on their **visibility**:
5355

@@ -116,3 +118,30 @@ target_link_libraries(DataProcessor PRIVATE
116118
StringUtils
117119
)
118120
```
121+
122+
## CMake Phases
123+
124+
CMake works in three phases:
125+
1. **Configure**: Execute `CMakeLists.txt` (find dependencies, create targets, OS/compiler-specific conditions, etc).
126+
2. **Generate**: Generate build files (propagates target properties and handles [_generator expressions_](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html)).
127+
3. **Build**: Execute the compiler to produce executables/libraries.
128+
129+
CMake keeps generated files separate from your source in a **build directory**. You specify a [**generator**](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) (Visual Studio/Ninja) and [**build type**](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations) (Debug/Release) during configuration.
130+
131+
```bash
132+
# Configure project and generate build files in build directory
133+
cmake -B build -DCMAKE_BUILD_TYPE=Release
134+
135+
# Build the project
136+
cmake --build build --config Release
137+
```
138+
139+
We specify both `CMAKE_BUILD_TYPE` and `--config` to be compatible with generators (like Visual Studio/Xcode) that allow multiple configurations at once.{:.info}
140+
141+
**Build Types**:
142+
- `Debug`: No optimization, debug symbols
143+
- `Release`: Full optimization, no debug symbols
144+
- `RelWithDebInfo`: Medium optimizations, debug symbols
145+
- `MinSizeRel`: Optimize for size, no debug symbols
146+
147+
**Generators**: CMake auto-detects (Makefiles on Linux/macOS, Visual Studio on Windows), but you can specify explicitly with `-G` during the _configure_ step.

0 commit comments

Comments
 (0)