CMake的 - 根据另一cmake的项目 [英] CMake - Depending on another cmake project

查看:654
本文介绍了CMake的 - 根据另一cmake的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构的一个项目我的工作:

I have the following structure to a project I am working on:

---Library1
------build
------include
------src
------CMakeLists.txt
---Library2
------build
------include
------src
------CMakeLists.txt
---Executable1
------build
------include
------src
------CMakeLists.txt

分享帮助是我开发的需要与 Library2 链接是第三方库的库。当我建立分享帮助,我需要它自动生成 Library2 并与之联系。 Executable1 将需要建立与分享帮助链接。我不知道如何与CMake的做的,我想知道是否有人能指导我在正确的方向。我想我可能需要使用 add_dependencies 命令或 add_subdirectory ,但我不知道如何去使用它们并确保它们与我的图书馆。任何帮助将是AP preciated。

Library1 is a library I am developing that needs to link with Library2 which is a 3rd party library. When I build Library1, I need it to automatically build Library2 and link with it. Executable1 will need to build and link with Library1. I'm not sure how to do with with Cmake, and I was wondering if anyone could guide me in the right direction. I think I may need to use the add_dependencies command or add_subdirectory but I'm not sure how to go about using them and making sure they are linked to my library. Any help would be appreciated.

推荐答案

我倒是觉得这里最好的命令很可能是的 add_subdirectory (当你怀疑)和的 target_link_libraries

I'd think the best commands here are likely to be add_subdirectory (as you suspected) and target_link_libraries.

我想与你的目录结构,我希望看到一个顶级的CMakeLists.txt根。在该文件的CMake,你会​​使用调用子目录'CMakeLists add_subdirectory

I guess with your directory structure, I'd expect to see a "top-level" CMakeLists.txt in the root. In that CMake file, you'd invoke the subdirectories' CMakeLists using add_subdirectory.

我想这两个分享帮助 Library2 是实际的CMake的目标,通过包括 add_library ,同样你有 add_executable(Executable1 ...)。在这种情况下,您可以添加以下内容分享帮助/的CMakeLists.txt:

I imagine both Library1 and Library2 are actual CMake targets, included via add_library, and similarly you have add_executable(Executable1 ...). In this case, you can add the following to Library1/CMakeLists.txt:

target_link_libraries(Library1 Library2)

现在的CMake自动链接 Library2 每当分享帮助指定为依赖。如果 Library2 被修改,那么它会自动链接到分享帮助之前再次重修。

CMake now automatically links Library2 whenever Library1 is specified as a dependency. If Library2 is modified, then it will be rebuilt automatically before being linked to Library1 again.

同样,在Executable1 /的CMakeLists.txt你可以再做:

Likewise in Executable1/CMakeLists.txt you can then do:

target_link_libraries(Executable1 Library1)

大概看这里的唯一的事情是, add_subdirectory 命令的顺序将需要

add_subdirectory(Library2)
add_subdirectory(Library1)
add_subdirectory(Executable1)

这样的依赖关系定义的的他们在 target_link_libraries 来电称。

这似乎很奇怪,我的最后一点是,你必须每个目标一个build目录。正常情况下应该只有需要一个build目录($ P源树之外pferably $)。

A final point which seems odd to me is that you have a build directory per target. Normally there should only be a need for a single build directory (preferably outside the source tree).

这篇关于CMake的 - 根据另一cmake的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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