Custom path at the end of RPATH

Hi there!

I kindly ask assistance with RPATH control in CMake. I would like to add custom path at the end of RPATH in every executable which gets produced (e.g. unittest executables) at build time (e.g. make and not install time e.g. make install).

This simple example demonstrates my wish:

cmake_minimum_required(VERSION 3.25)
project(rpath)

# This will place "/foo/bar" at the front of RPATH but I need it at the end
set(CUSTOM_RPATH_VAR "/foo/bar")
set(CMAKE_BUILD_RPATH ${CUSTOM_RPATH_VAR})

find_package(Qt5 5.15 COMPONENTS Core REQUIRED)

file(WRITE "main.c" "int main() { return 0; }")

add_executable(test1 main.c)
target_link_libraries(test1 Qt5::Core)

add_executable(test2 main.c)
target_link_libraries(test2 Qt5::Core)

After compilation RPATH of generated executables, as expected, looks like this:

$ chrpath -l test1
test1: RUNPATH=/foo/bar:/opt/Qt5.15.5/lib

What can I do to put my custom path at the end of RPATH via CMake? What options do I have?

Thank you so much,
Goran

I don’t think there’s a mechanism for rpath ordering yet. Probably worth a feature request (or searching for an existing one).