https://github.com/retifrav/cmake-target-link-libraries-example
An example of CMake target_link_libraries() scopes effects
https://github.com/retifrav/cmake-target-link-libraries-example
cmake cpp example
Last synced: 5 months ago
JSON representation
An example of CMake target_link_libraries() scopes effects
- Host: GitHub
- URL: https://github.com/retifrav/cmake-target-link-libraries-example
- Owner: retifrav
- License: gpl-3.0
- Created: 2023-07-14T18:19:55.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-07T15:44:13.000Z (over 2 years ago)
- Last Synced: 2024-05-01T21:33:09.850Z (over 1 year ago)
- Topics: cmake, cpp, example
- Language: CMake
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# CMake target_link_libraries() scopes example
An example of particular effects from `PRIVATE`/`INTERFACE`/`PUBLIC` scope in CMake's `target_link_libraries()`.
More details in the [following article](https://decovar.dev/blog/2023/07/22/cmake-target-link-libraries-scopes/).
Unlike the code fragments in the article, the `prjct` here handles linking scope via `THINGY_LINKING` cache variable, for example `-DTHINGY_LINKING="INTERFACE"`. Based on that, certain target properties are set and carried over, changing certain compile definitions, making the compilation to account for the current linking scope "automatically" (*so you don't need to modify the sources yourself*).
## Building
It is 3 projects that are built individually and in particular order:
1. `dpndnc` - a library made to be a direct dependency of `prjct`;
2. `prjct` - a project with two libraries, one of which directly depends on `dpndnc`;
3. `tl` - a CLI tool that directly depends on `prjct` libraries and also transitively depends on `dpndnc`.
### dpndnc
``` sh
$ cd /path/to/cmake-target-link-libraries-example/dpndnc
$ mkdir build && cd $_
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="../install" ..
$ cmake --build . --target install
```
### prjct
``` sh
$ cd /path/to/cmake-target-link-libraries-example/prjct
$ mkdir build && cd $_
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="../install" \
-DCMAKE_PREFIX_PATH="/path/to/cmake-target-link-libraries-example/dpndnc/install" \
-DTHINGY_LINKING="INTERFACE" \
..
$ cmake --build . --target install
```
### tl
``` sh
$ cd /path/to/cmake-target-link-libraries-example/tl
$ mkdir build && cd $_
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="../install" \
-DCMAKE_PREFIX_PATH="/path/to/cmake-target-link-libraries-example/prjct/install;/path/to/cmake-target-link-libraries-example/dpndnc/install" \
..
$ cmake --build . --target install
$ ../install/bin/some-tool
```