[CMake] setting rpath in executable in build directory

Jędrzej Dudkiewicz jedrzej.dudkiewicz at gmail.com
Fri Oct 18 10:34:32 EDT 2019


Hello,

I have the following scenario. I have gcc 9.2.0 and boost 1.71.0
installed in /opt/ZS/deps/... directory, built for both arm and
x86_64. I can build programs using them just fine and they run as
expected provided -Wl,-rpaht,/opt/ZS/deps... is added to g++ command
line.

Now I'm trying to build  mqtt_cpp library
(https://github.com/redboltz/mqtt_cpp) using them. Everything builds
just fine using:

export CXX=x86_64-linux-gnu-g++-9.2.0
cmake -G'Unix Makefiles' -S. -Bbuild \
    -DBOOST_ROOT=/opt/ZS/deps/include/ \
    -DBOOST_LIBRARYDIR=/opt/ZS/deps/x86_64-linux-gnu/lib/

This of course does not contain any information regarding RPATH. I
assumed this is why binaries (examples in fact) in build directory
can't find proper libraries, so I tried installing them (examples).
Unfortunately "make install" installs library (it is header-only
library) and not examples, so I can't run any of them.

Since I *really* want to run these examples, I tried configuring and
building using this command:

export CXX=x86_64-linux-gnu-g++-9.2.0
cmake -G'Unix Makefiles' -S. -Bbuild \
    -DBOOST_ROOT=/opt/ZS/deps/include/ \
    -DBOOST_LIBRARYDIR=/opt/ZS/deps/x86_64-linux-gnu/lib/ \
    -DCMAKE_SKIP_BUILD_RPATH=FALSE \
    -DCMAKE_SKIP_RPATH=OFF \
    -DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE

I assumed that -DCMAKE_SKIP_BUILD_RPATH=FALSE means that I want
binaries in build directory (not installed ones) to already have RPATH
embedded.
I also assumed that -DCMAKE_SKIP_RPATH=OFF assures me that RPATH will
be used during build and/or installation.
I assumed that -DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE is supposed to
add to binaries in build directory RPATH containing paths to libraries
found and used by CMake.

All of this simply isn't true, binaries generated by "make -j 4" not
only don't have proper RPATH, they don't have rpath at all, not even
invalid one. I tried with:

readelf -a build/example/broker | grep -i rpath

and using chrpath:

chrpath build/exampl/broker

but there is not even a trace of rpath. Greps comes back emptyhanded
and `chrpath` util informs me, that:

broker: no rpath or runpath tag found

Is there some some magical combination of these flags that allows me
to build and run (this is paramount) binaries that were not installed?

CMake used is 3.15.4, I don't think earlier ones are aware of boost's
version 1.71.0.

I also tried adding -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON , but it
changes nothing.

Thanks in advance,
-- 
Jędrzej Dudkiewicz

I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.


More information about the CMake mailing list