CMake自动链接外部库依赖项
我目前正在开发一个库 HAMMER,并使用 CMake 作为其构建系统。我正在集成 YARP,它也使用 CMake 作为其构建系统。因此,我使用标准 CMake 方法导入 YARP,即:将 CMake 指向在 YARP 编译期间生成的 YARPConfig.cmake
文件,并使用 YARP_LIBRARIES
和 YARP_INCLUDE_DIRS
。到目前为止,一切都很好。
问题是,在(我的库的)链接阶段,CMake 将自动链接 YARP 依赖的所有库。例如,YARP 使用 gsl,但我的库没有(最多调用使用 gsl 的 YARP 方法),但 gsl 显示为我的库的 ld 依赖项之一(使用 ldd)。
当使用默认库路径中的库时,这不是什么大问题,但如果您需要链接不在库路径上的库(并且不属于您的项目),则链接将失败(因为找不到所需的库)。
这正是尝试构建使用 HAMMER(也使用 CMake)的应用程序时发生的情况,由于 YARP 未安装在默认位置,我的应用程序尝试链接它(按照刚才描述的行为)并失败,因为它找不到 YARP 库。
有谁知道为什么 CMake 有这个政策?而且,更重要的是,有没有一种方法可以解决这个问题,并且不需要将 YARP 导入到应用程序的 CMake 脚本中?
谢谢!
I am currently developing a library, HAMMER, and using CMake as its build system. I'm in the process of integrating YARP which also uses CMake as its build system. Consequently I import YARP using the standard CMake method, ie. pointing CMake to a YARPConfig.cmake
file that was generated during YARP compilation and using YARP_LIBRARIES
and YARP_INCLUDE_DIRS
. So far, so good.
The problem is that during the linking stage (of my library) CMake will automatically link against all the libraries that YARP depends upon. For example YARP uses gsl but my library does not (at most it calls the YARP methods that use gsl), yet gsl appears listed as one the ld dependencies of my library (using ldd
).
This is not much of an issue when using libraries that are in the default library path, but if you need to link against a library that is not on the library path (and that is not part of your project), then linking will fail (as the required library won't be found).
That is exactly what is happens when trying to build an application that uses HAMMER (also with CMake), since YARP is not installed in the default location, my application tries to link against it (as per the behaviour just described) and fails because it cannot find the YARP libraries.
Does anyone know why CMake has this policy? And, more importantly, is there a way around this that doesn't imply importing YARP into the CMake scripts of the application?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 CMake 常见问题解答为什么当某些链接到我的共享库时会包含链接到它的库?。如果你想阻止这种行为,我认为你必须更改 YARP 的 LINK_INTERFACE_LIBRARIES 属性。
See the CMake FAQ Why are libraries linked to my shared library included when something links to it?. If you want to prevent that behavior, I think you have to change the LINK_INTERFACE_LIBRARIES property of YARP.