CMake:不要为链接中使用的单个库设置rpath [英] CMake: Don't set rpath for a single library used in link

查看:173
本文介绍了CMake:不要为链接中使用的单个库设置rpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

具体地说,我想在没有安装libOpenCL.so作为系统库的构建服务器场上的构建期间链接到libOpenCL.so.为此,libOpenCL.so位于项目构建树中,并使用CMakeLists文件中的绝对路径进行引用.此绝对路径是为了确保如果系统确实安装了libOpenCL.so,则不会使用它.

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.

但是,当运行最终的可执行文件时,CMake已将绝对路径添加到rpath中,该路径停止了libOpenCL.system版本,因此由库加载器拾取并使用.

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.

谢谢!

推荐答案

我知道这个答案太迟了.我面临着与您相同的要求.我们需要的是白名单方法,其中可以根据需要明确设置 CMAKE_BUILD_RPATH .或者,我们需要一种黑名单方法来告诉cmake,我们不希望在可执行文件中使用哪个 RPATH .尚未记录从构建树中删除 RPath 的方法: https://gitlab.kitware.com/cmake/cmake/issues/16825

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

我采取的解决方案是:

设置 RUNPATH 而不是 RPATH .您可以通过以下语句实现这一点:

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

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

如果存在 RUNPATH ,则将忽略 RPATH . RUNPATH -与 RPATH 相同,但在 LD_LIBRARY_PATH 之后搜索,仅在最新的UNIX上受支持

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

然后我可以使用环境变量 LD_LIBRARY_PATH 来实现覆盖库.

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

这篇关于CMake:不要为链接中使用的单个库设置rpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆