aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/development/cmake.md
blob: d0bbeeaf7fcb6d21188baf6228e6604dbb4cae86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# cmake(1)

## `PRIVATE` / `PUBLIC` / `INTERFACE`

These modifier control where properties for a given target are visible.

- `PRIVATE`: Only for the target itself.
- `PUBLIC`: For the target itself and anyone linking against it.
- `INTERFACE`: Only for anyone linking against the target.

The following gives an example for preprocessor definitions specified on a
library target. This behaves in the same way for other properties like for
example include directories.
```cmake
# CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(moose)

# -- LIBRARY
add_library(liba STATIC liba.cc)
target_compile_definitions(liba PUBLIC DEF_PUBLIC)
target_compile_definitions(liba PRIVATE DEF_PRIVATE)
target_compile_definitions(liba INTERFACE DEF_INTERFACE)

# -- APPLICATION
add_executable(main main.cc)
target_link_libraries(main liba)
```

```sh
> touch liba.cc; echo "int main() {}" > main.cc
> cmake -B build -S . -G Ninja
> ninja -C build -j1 --verbose
[1/4] /usr/bin/c++ -DDEF_PRIVATE -DDEF_PUBLIC  [..] .../liba.cc
[2/4] [..]
[3/4] /usr/bin/c++ -DDEF_INTERFACE -DDEF_PUBLIC [..] .../main.cc
[4/4] [..]
```