简体   繁体   English

CMake:不要为链接中使用的单个库设置rpath

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

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. 我想要做的是配置我的CMakeLists文件,以便在构建我的项目时,链接器使用驻留在我的构建树中的共享库(.so)的副本来链接可执行文件但不设置rpath链接的可执行文件,以便系统必须在加载程序请求时提供库。

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. 具体来说,我想在没有安装libOpenCL.so作为系统库的构建服务器场的构建期间链接libOpenCL.so。 To do this, libOpenCL.so is in the project build tree and referenced using an absolute path in the CMakeLists file. 为此,libOpenCL.so位于项目构建树中,并使用CMakeLists文件中的绝对路径进行引用。 This absolute path is to ensure that if the system does happen to have libOpenCL.so installed then it is not used. 这个绝对路径是为了确保如果系统确实安装了libOpenCL.so,那么就不会使用它。

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. 但是,当运行最终的可执行文件时,CMake已经添加了rpath的绝对路径,该路径停止了libOpenCL的系统版本。由库加载器拾取并使用。

Seems simple but I can't quite figure it out. 看似简单但我无法弄明白。

Thanks! 谢谢!

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. 我们需要的是白名单方法,我们将CMAKE_BUILD_RPATH明确地设置为我们需要的方法。 Or we need a blacklist approach where we tell cmake, which RPATH s we don't want in the executable. 或者我们需要一个黑名单方法,我们告诉cmake,我们在可执行文件中不需要哪个RPATH Way to remove RPath from build tree is not documented yet: https://gitlab.kitware.com/cmake/cmake/issues/16825 尚未RPath从构建树中删除RPath方法: httpsRPath

The solution I took is: 我采取的解决方案是:

Set RUNPATH instead of RPATH . 设置RUNPATH而不是RPATH You can achieve this by the statement: 您可以通过以下声明实现此目的:

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

When RUNPATH is present, RPATH is ignored. 当存在RPATH将忽略RUNPATH RUNPATH - same as RPATH , but searched after LD_LIBRARY_PATH , supported only on most recent UNIX RUNPATH -同RPATH ,但经过搜索LD_LIBRARY_PATH ,仅在最近的UNIX支持

Then I can achieve the overriding the library using the environment variable LD_LIBRARY_PATH . 然后我可以使用环境变量LD_LIBRARY_PATH来覆盖库。

According to the CMake Wiki this should not be a problem: 根据CMake Wiki,这应该不是问题:

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. 默认情况下,如果不更改任何与RPATH相关的设置,CMake会将可执行文件和共享库与完整的RPATH链接到构建树中的所有已使用的库。 When installing, it will clear the RPATH of these targets so they are installed with an empty RPATH. 安装时,它将清除这些目标的RPATH,以便安装空RPATH。

So you might try to simply install it? 所以你可能会尝试简单地安装它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM