CMake: Don't set rpath for a single library used in link

落花浮王杯 提交于 2021-02-07 12:48:22

问题


What I'd like to do is configure my CMakeLists file so that while building my project the linker uses a copy of a shared library (.so) that resides in my build tree to link the executable against but then does not set the rpath in the linked executable so that the system must provide the library when the loader requests it.

Specifically, I want to link against libOpenCL.so during build time on a build farm that doesn't have libOpenCL.so installed as a system library. To do this, libOpenCL.so is in the project build tree and referenced using an absolute path in the CMakeLists file. This absolute path is to ensure that if the system does happen to have libOpenCL.so installed then it is not used.

However, when running the final executable, CMake has added the absolute path to the rpath which stops the system version of libOpenCL.so being picked up by the library loader and used.

Seems simple but I can't quite figure it out.

Thanks!


回答1:


I know this answer is super late. I faced the same requirement as yours. Either we need is whitelist approach where we set CMAKE_BUILD_RPATH explicitly with what we need. Or we need a blacklist approach where we tell cmake, which RPATHs we don't want in the executable. Way to remove RPath from build tree is not documented yet: https://gitlab.kitware.com/cmake/cmake/issues/16825

The solution I took is:

Set RUNPATH instead of RPATH. You can achieve this by the statement:

SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags")

When RUNPATH is present, RPATH is ignored. RUNPATH - same as RPATH, but searched after LD_LIBRARY_PATH, supported only on most recent UNIX

Then I can achieve the overriding the library using the environment variable LD_LIBRARY_PATH.




回答2:


According to the CMake Wiki this should not be a problem:

By default if you don't change any RPATH related settings, CMake will link the executables and shared libraries with full RPATH to all used libraries in the build tree. When installing, it will clear the RPATH of these targets so they are installed with an empty RPATH.

So you might try to simply install it?



来源:https://stackoverflow.com/questions/35776048/cmake-dont-set-rpath-for-a-single-library-used-in-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!