cmake find_package 由于多次查找相同的包而变慢

发布于 2025-01-11 10:06:21 字数 1419 浏览 0 评论 0原文

我有一个使用 CMake 构建的 C++ 项目,如下所示:

  • 顶级项目通过调用 add_subdirectory 添加每个子模块
  • ,每个子模块通过调用 add_subdirectory 添加自己的子模块
  • ,最后,子模块调用setup_src函数,其中调用find_package来搜索不同的依赖项。
  • 一些依赖项是相同的,但由不同的模块使用
  • 问题:我想避免为不同的模块多次使用相同依赖项的find_package,我该怎么做?

为了让您有一个全面的了解,这里是用于调查此问题的 cmake 跟踪可视化。 输入图片这里的描述

更多细节

我有一个用CMake构建的C++项目,这个项目有很多子模块,它们具有类似的项目结构,我使用CMake的add_subdirectory 递归添加每个子模块及其自己的子模块。

在每个子模块中,我都有一个 CMake 函数 setup_src,我在其中调用 find_package 来搜索该子模块的依赖项。一些依赖项由多个子模块共享,我发现整个 CMake 配置过程很慢(17 秒),我做了一些分析并多次找到相同共享依赖项的 find_package ,这使得配置慢。例如,modA 依赖于 find_package(gRPC),modB 也依赖于 find_package(gRPC),而我期望的是第二个 find_package(gRPC)要么不会发生,要么发生得非常快,但事实并非如此。

我试图通过在 CMake 中使用额外的保护标准来避免第二次 find_package 相同的依赖项,因为我假设 modA 已经找到 gRPC 并且 modB 不必再次找到它,但 CMake 报告错误,显示类似 CMake Error at xxxx.cmake:578 (_add_library): Target "zzz" links to target "yyy::yyy" but the target was not found。也许 IMPORTED 目标缺少 find_package() 调用,或者缺少 ALIAS 目标?,并且我知道 yyy 是共享依赖项 (gRPC< /code> 在示例中),但我不清楚如何避免多次找到相同的包而不遇到此问题。

任何帮助表示赞赏。谢谢。

I have a C++ project built using CMake like this way:

  • The top level project adds each sub module by calling add_subdirectory
  • and each sub module add its own sub modules by calling add_subdirectory
  • and in the end, the sub module calls setup_src function, where find_package is called to search different dependencies.
  • Some of the dependencies are the same but used by different modules
  • Question: I would like to avoid find_package for the same dependeny multiple times for different modules, how can I do that?

To give you a big picture, here is the cmake tracing visualization for investigating this issue.
enter image description here

More Details

I have a C++ project built with CMake, and this project has many sub modules, which has similar project structure, and I use CMake's add_subdirectory to add each sub module and their own sub modules recursively.

In each sub module, I have a CMake function setup_src, where I call find_package for searching for this sub module's dependencies. Some of the dependencies are shared by multiple sub modules, I find the overall CMake configuration process slow (17 seconds), and I did some profiling and found the find_package for the same shared dependencies multiple times, which makes the configuration slow. For example, modA depends on find_package(gRPC) and modB also depends on find_package(gRPC), and I expected the second find_package(gRPC) either not happen or be very fast, but it turns out not the case.

I tried to avoid the second time find_package for the same dependency by using additional guard criteria in CMake since I assumed modA already found gRPC and modB doesn't have to find it again, but errors are reported by CMake saying something like CMake Error at xxxx.cmake:578 (_add_library): Target "zzz" links to target "yyy::yyy" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?, and I know yyy is a transitive dependency for the shared dependency (gRPC in the example), but it is not clear to me how I can avoid finding the same package multiple times while not running into this issue.

Any help is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文