[CMake] Suppress a specific path in the RPATH of an executable explicitly

Eric Dönges doenges at mvtec.com
Thu Dec 19 05:52:55 EST 2019


On 19.12.19 10:52, Cornelis Bockemühl wrote:
> In a project setup I am copying a number of shared library files into
> the executable directory (basically with a mechanism involving
> configure_file - because the "install" logic was simply too complicated
> for the project and for my limited brain capacity!).
> 
> On a Windows system this works nicely, but on a Linux system there is
> always some logic that puts the original shared library location into
> the executable's RUNPATH, with the effect that they are not found in the
> place where they are supposed to be found.
> 
> I tried to avoid this by using
> 
> set_target_properties(
>     <imported targets>
>     PROPERTIES
>         CMAKE_SKIP_RPATH TRUE)

This cannot work because CMAKE_SKIP_RPATH is a variable, not a property.
Variables are set with the set() command (or from the command line with
the -D option), and often serve as the default values for a specific
property if that property isn't explicitly set. Properties are set with
set_property() or set_target_properties() and apply only to the specific
target they are set for. In general, anything with a 'CMAKE_' prefix is
a variable, not a property.

Note that CMake distinguishes between "build time" and "install time"
rpath, allowing you to have different rpaths for the binaries in your
build and install directories. So if you do not want any rpath
information added to your binaries, you would

set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_SKIP_INSTALL_RPATH TRUE)

before defining any targets.

If you want to control the rpath for individual binaries, you would set
the appropriate properties for the binary target(s) whose rpath you want
to set. Check the CMake documentation for properties containing 'RPATH'
to see which properties are available - there is quite a number of them.
As far as I know, these properties are not transitive, so you cannot set
the rpath for a shared library and expect targets linked against that
library to receive this rpath.

It sounds to me that what you probably want to do is set the RPATH of
your executables to look in $ORIGIN and nowhere else. One way to do this
would be to

set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "$ORIGIN")

before defining your targets. If you want control over individual
targets, you would set the BUILD_WITH_INSTALL_RPATH and INSTALL_RPATH
properties on the individual targets.

For more details, see
https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling.



More information about the CMake mailing list