Linux | macOS | Windows |
---|---|---|
- GNU/Linux
- MacOS
- Windows
This is a complete example of how to create a Modern CMake C++ Project.
This project should run on GNU/Linux, MacOS and Windows.
To complexify a little, the CMake project is composed of three libraries (Foo
, Bar
and FooBar
)
and one executable FooBarApp
with the following dependencies:
CMakeSwig::Foo:
CMakeSwig::Bar:
CMakeSwig::FooBar: PUBLIC CMakeSwig::Foo PRIVATE CMakeSwig::Bar
CMakeSwig::FooBarApp: PRIVATE CMakeSwig::FooBar
To build the C++ project, as usual:
cmake -S. -Bbuild
cmake --build build --target all -v
Since we want to use the CMAKE_BINARY_DIR to generate the wrapper package (e.g. python wheel package) as well as be able to test from the build directory. We need to enable:
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
And have a finely tailored rpath for each library.
For Foo
and Bar
which depend on nothing:
set(CMAKE_INSTALL_RPATH "$ORIGIN")
For FooBar
which depend on Foo
and Bar
:
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../Foo:$ORIGIN/../Bar")
For FooBarApp
which depend on FooBar
:
include(GNUInstallDirs)
...
set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN/../FooBar")