By trial and error, I found out that my current (yeah, it should be -rpath
rather than --rpath
…):
add_link_options(
LINKER:-rpath,<rpath-path>
LINKER:-dynamic-linker,<dynamic-linker-path>
)
could be replaced in a few ways revolving around various variables.
As of now, I’m going to use:
LDFLAGS="${LDFLAGS} -Wl,-dynamic-linker,<dynamic-linker-path>"
CMAKE_BUILD_RPATH=<rpath-path>
LDFLAGS
is an environment variable while CMAKE_BUILD_RPATH
is provided as -D<var>=<value>
command-line argument do cmake
configuration call.
However, this particular choice is dictated mostly by the setup we have right now. In a different setup perhaps a different approach would be better.
For completeness, I will list here variables that seem to be related to the topic:
- environment variables:
–LDFLAGS
- CMake variables:
–CMAKE_SKIP_RPATH
–CMAKE_BUILD_RPATH
–CMAKE_BUILD_RPATH_USE_ORIGIN
–CMAKE_BUILD_WITH_INSTALL_RPATH
–CMAKE_EXE_LINKER_FLAGS
–CMAKE_EXE_LINKER_FLAGS_<CONFIG>
–CMAKE_EXE_LINKER_FLAGS_<CONFIG>_INIT
–CMAKE_EXE_LINKER_FLAGS_INIT
–CMAKE_INSTALL_REMOVE_ENVIRONMENT_RPATH
–CMAKE_INSTALL_RPATH
–CMAKE_INSTALL_RPATH_USE_LINK_PATH
–CMAKE_MODULE_LINKER_FLAGS
–CMAKE_MODULE_LINKER_FLAGS_<CONFIG>
–CMAKE_MODULE_LINKER_FLAGS_<CONFIG>_INIT
–CMAKE_MODULE_LINKER_FLAGS_INIT
–CMAKE_SHARED_LINKER_FLAGS
–CMAKE_SHARED_LINKER_FLAGS_<CONFIG>
–CMAKE_SHARED_LINKER_FLAGS_<CONFIG>_INIT
–CMAKE_SHARED_LINKER_FLAGS_INIT
–CMAKE_SKIP_BUILD_RPATH
–CMAKE_SKIP_INSTALL_RPATH
–CMAKE_LANG_LINKER_WRAPPER_FLAG
–CMAKE_LANG_LINKER_WRAPPER_FLAG_SEP
TBH, I’m somewhat lost in all those options… However, I will share some thoughts on them I have after spending significant time on this topic:
- I don’t like setting environment variable
LDFLAGS
(because I don’t like dependency on environment variables in general).
– However, it does save me from setting three variables:CMAKE_EXE_LINKER_FLAGS_INIT
,CMAKE_MODULE_LINKER_FLAGS_INIT
, andCMAKE_SHARED_LINKER_FLAGS_INIT
.
– If using aCMAKE_TOOLCHAIN_FILE
, as already suggested by @fenrir, maybe I would opt for separate variables, but having to pass them all via-D<var>=<value>
seems a bit too verbose.
– We useLDFLAGS
already anyway for other flags, I’m not sure how it would behave if I would separately set theCMAKE_<TYPE>_LINKER_FLAGS_INIT
variables but I expect it would overwrite everything that would be taken fromLDFLAGS
which is not desired. - I don’t know if it is better to set
rpath
by linker flags or by CMake variables.
– I assume CMake variables are better since it will lead to a cleaner situation from CMake’s point of view and avoid conflicts.
– And also they are more fine-grained. It is easier to have a single variable with single path of clear purpose rather than append to a cumulative options variable. - The
add_link_options
has a niceLINKER:
mode that covers specifics of the compiler (for example,-Wl
for GCC and-Xlinker
for Clang).
– When setting the variables manually within CMake you can still useCMAKE_LANG_LINKER_WRAPPER_FLAG
andCMAKE_LANG_LINKER_WRAPPER_FLAG_SEP
. Probably non-trivial and verbose to use it correctly, but at least possible.
– However, when setting them from the outside (which is the whole point of setting them in the first place) you now must cover the compiler specifics on your own and the same settings will not work with both GCC and Clang.