当使用-L添加link_library时,cmake未设置RPATH [英] Cmake not setting RPATH when adding link_library with -L

查看:759
本文介绍了当使用-L添加link_library时,cmake未设置RPATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以以下方式设置链接库时

target_link_libraries (SOME_TARGET -L/somedir -lfoo)

cmake不处理RPATH.使用'-L'和'-l'不是最佳实践,还是实际上是错误的?创建我自己的Find * .cmake时,我通常使用find_library(),但是我得到的find脚本不会执行此操作,而是使用'-L'和'-l'来使用上述形式.

该文档并没有真正说明如何收集RPATH,也没有明确说明如何处理"-l"和"-L"唯一获得的指针

以-开头的项目名称,但不以-l或-framework开头的项目名称被视为 链接器标志"

解决方案

通常不建议指定与工具链相关的标志,例如-l-L,因为它破坏了可移植性,并且可能会产生与您预期不同的效果.

设置链接器路径的正确方法是 link_directories 命令.

CMake的惯用解决方案是使用find_library定位库,然后将完整路径传递给链接器,因此您完全不必担心链接目录.

现在,RPATH是另一种野兽,因为它还决定了动态库在运行时可以位于何处.通常,默认设置在这里可以正常工作.如果您发现自己处在不幸的境地,而并非如此,那么有很多目标属性和CMake变量会影响到这一点:

有一些用于指定RPATH规则的属性. INSTALL_RPATH 是用分号分隔的列表,指定要在已安装的rpath中使用 目标(针对支持它的平台). INSTALL_RPATH_USE_LINK_PATH 是一个布尔值,如果设置为true,将在链接器中追加目录 搜索路径,并在项目外部找到INSTALL_RPATH. SKIP_BUILD_RPATH是一个布尔值,指定是否自动跳过 rpath的生成,允许目标从构建树运行. BUILD_WITH_INSTALL_RPATH是一个布尔值,指定是否链接 使用INSTALL_RPATH在构建树中定位目标.这优先 通过SKIP_BUILD_RPATH并避免在重新链接之前需要 安装. INSTALL_NAME_DIR是指定目录的字符串 Mac OSX上共享库的"install_name"字段的一部分 在已安装的目标中使用.创建目标时的值 变量CMAKE_INSTALL_RPATH,CMAKE_INSTALL_RPATH_USE_LINK_PATH, CMAKE_SKIP_BUILD_RPATH,CMAKE_BUILD_WITH_INSTALL_RPATH和 CMAKE_INSTALL_NAME_DIR用于初始化这些属性.

(从 set_target_properties文档 )

此外,您可能想看看用于RPATH处理的CMake Wiki页面.

不幸的是,整个RPATH业务非常复杂,而详尽的解释将需要比StackOverflow答案更多的空间,但是我希望这足以使您入门.

When setting link libraries in the following manner

target_link_libraries (SOME_TARGET -L/somedir -lfoo)

cmake doesn't handle RPATHs. Is using '-L' and '-l' not best practice, or actually plain wrong? When creating my own Find*.cmake I usually use find_library() but the find script I got doesn't do this and resorts to the above form using '-L' and '-l'.

The documentation doesn't really explain how RPATHs are gathered, also the documentation isn't really clear how it handles "-l" and "-L" the only pointer you get is

"Item names starting with -, but not -l or -framework, are treated as linker flags"

解决方案

Specifying toolchain-dependent flags like -l and -L is generally not recommended, as it breaks portability and might have different effects than you expect.

The correct way to set the linker path would be the link_directories command.

The idiomatic solution in CMake is to use find_library for locating the library and then pass the full path to the linker, so you do not need to worry about link directories at all.

Now, the RPATH is a different beast, as it also determines where dynamic libraries can be located at runtime. Usually, the default settings work reasonably fine here. If you ever find yourself in the unfortunate situation where it does not, there is a number of target properties and CMake variables influencing this:

There are a few properties used to specify RPATH rules. INSTALL_RPATH is a semicolon-separated list specifying the rpath to use in installed targets (for platforms that support it). INSTALL_RPATH_USE_LINK_PATH is a boolean that if set to true will append directories in the linker search path and outside the project to the INSTALL_RPATH. SKIP_BUILD_RPATH is a boolean specifying whether to skip automatic generation of an rpath allowing the target to run from the build tree. BUILD_WITH_INSTALL_RPATH is a boolean specifying whether to link the target in the build tree with the INSTALL_RPATH. This takes precedence over SKIP_BUILD_RPATH and avoids the need for relinking before installation. INSTALL_NAME_DIR is a string specifying the directory portion of the "install_name" field of shared libraries on Mac OSX to use in the installed targets. When the target is created the values of the variables CMAKE_INSTALL_RPATH, CMAKE_INSTALL_RPATH_USE_LINK_PATH, CMAKE_SKIP_BUILD_RPATH, CMAKE_BUILD_WITH_INSTALL_RPATH, and CMAKE_INSTALL_NAME_DIR are used to initialize these properties.

(From the set_target_properties docs)

Also, you might want to have a look at the CMake Wiki page for RPATH handling.

The whole RPATH business is unfortunately rather complex and a thorough explanation would require far more space than is appropriate for a StackOverflow answer, but I hope this is enough to get you started.

这篇关于当使用-L添加link_library时,cmake未设置RPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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