CMake自动链接外部库依赖项

发布于 2024-12-03 18:48:48 字数 705 浏览 0 评论 0原文

我目前正在开发一个库 HAMMER,并使用 CMake 作为其构建系统。我正在集成 YARP,它也使用 CMake 作为其构建系统。因此,我使用标准 CMake 方法导入 YARP,即:将 CMake 指向在 YARP 编译期间生成的 YARPConfig.cmake 文件,并使用 YARP_LIBRARIESYARP_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文