An example of particular effects from PRIVATE
/INTERFACE
/PUBLIC
scope in CMake's target_link_libraries()
.
More details in the following article.
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).
It is 3 projects that are built individually and in particular order:
dpndnc
- a library made to be a direct dependency ofprjct
;prjct
- a project with two libraries, one of which directly depends ondpndnc
;tl
- a CLI tool that directly depends onprjct
libraries and also transitively depends ondpndnc
.
$ 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
$ 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
$ 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