Skip to content

Latest commit

 

History

History
64 lines (52 loc) · 2.17 KB

cpp.md

File metadata and controls

64 lines (52 loc) · 2.17 KB
Linux macOS Windows
Status Status Status

C++ Status

  • GNU/Linux
  • MacOS
  • Windows

Introduction

This is a complete example of how to create a Modern CMake C++ Project.

This project should run on GNU/Linux, MacOS and Windows.

Dependencies

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

C++ Project Build

To build the C++ project, as usual:

cmake -S. -Bbuild
cmake --build build --target all -v

Managing RPATH

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")