"CMAKE_INSTALL_RPATH $ORIGIN" expands to empty

Hi, I have an utils.cmake file which is used by all my projects and some of the lines were

set(CMAKE_SHARED_LINKER_FLAGS “${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath,‘$ORIGIN’”)
set(CMAKE_EXE_LINKER_FLAGS “${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,‘$ORIGIN’”)

Which was expanded to -Wl,-rpath,'$ORIGIN' and works perfectly.
Now I am trying to modernize my code and, based on the Professional CMake book, I replaced it with:

set(CMAKE_INSTALL_RPATH $ORIGIN)

But it seems that for some of the files it expands to -Wl,-rpath,:::::::, and I don’t understand why.

Here is a comparison of the flags before and after.

Should I do something special? Maybe put that line first or last, or add something? Should I put it for all the targets? Not sure how should I make it work

Thanks

The variable which you’re using is used to pre-initialise the property INSTALL_RPATH when a target is created, so if you want to use the variable, you must set it before you create the targets to which it should apply. Alternatively, you can set the property on the targets directly (after you create them).

Second, note that the variable affects install RPATH; that is, RPATH that will be set by CMake as part of the install step (make install or equivalent). If you want to use the same RPATH also during the build, look into the property BUILD_WITH_INSTALL_RPATH, which can similarly be pre-initialised with variable CMAKE_BUILD_WITH_INSTALL_RPATH.

Alternatively, you can modify the build RPATH directly using property BUILD_RPATH, which in turn can be pre-initialised using variable CMAKE_BUILD_RPATH.

2 Likes

Scanning CMake docs for an unrelated thing, I came upon property BUILD_RPATH_USE_ORIGIN, which might also be relevant to your case.

1 Like

@Angew thanks a lot for your great help, it seems that I needed this line added set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

Meanwhile I found that CMake 3.15 (which I use) has a problem with escaping RPATH, so I am not sure if I should escape the $ or not, like it’s recommended here: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling

Now I am trying to make it work on macos also
this set(CMAKE_INSTALL_RPATH "@loader_path"), although it expands to -Wl,-rpath,"\$ORIGIN", the executable cannot find the libraries.
Library not loaded: @rpath/libfile.dylib

Hi,

Well, I’m hitting the same issue than @Taw_Moto.

No matter how I try to set INSTALL_RPATH to $ORIGIN with the CMAKE_INSTALL_RPATH command (tried various syntax such as $ORIGIN, "\$ORIGIN", "${ORIGIN}", \\\$ORIGIN, '$ORIGIN', …), I’m always getting ::::::: (the number of colons depends on the used syntax) as RUNPATH entry in the target’s binary file. It’s noteworthy that the target’s cmake_install.cmake script correctly mirrors the used syntax $ORIGIN, "\$ORIGIN", "${ORIGIN}", \\\$ORIGIN, '$ORIGIN', … as RPATH in the file(RPATH_CHECK statement. Since it differs from the RUNPATH ::::::: entry in the binary file, the latter get deleted on install.

I’ve double-checked: the target is definitely created after the call to CMAKE_INSTALL_RPATH command.

By contrast, as outlined by @Taw_Moto, setting RPATH to $ORIGIN using the CMAKE_EXE_LINKER_FLAGS command yields to a correct RUNPATH $ORIGIN entry in the target’s binary file, matching the RPATH value expected by the file(RPATH_CHECK statement in the cmake_install.cmake script and the executable thus installs flawlessly.

What am I missing here? This is with CMake 3.26.5 as provided with Red Hat Enterprise Linux 8. I’m stuck with this version because of IT requirements.

Thanks.

RPATHs are rewritten on install, the colons are placeholder characters. Perform the install and the RPATHs will be rewritten into the binaries.