[英]How do I share headers and libraries in subdirectory by cmake?
I want to use my headers and libs as common libraries for app1 and app2.我想使用我的头文件和库作为 app1 和 app2 的公共库。 My project tree are below.我的项目树如下。 image/
and math/
are library directories used by app1 and app2. image/
和math/
是 app1 和 app2 使用的库目录。 In this case, should I set same settings to both CmakeLists.txt
under app1 and app2?在这种情况下,我应该为 app1 和 app2 下的CmakeLists.txt
设置相同的设置吗? Of course I know it works, but are there any smarter ways to set common libraries?我当然知道它有效,但是有没有更聪明的方法来设置公共库?
|-- CMakeLists.txt
|-- app1
| |-- CMakeLists.txt
| `-- main.cc
|-- app2
| |-- CMakeLists.txt
| `-- main.cc
|-- image
| |-- CMakeLists.txt
| |-- include
| | `-- image_func.h
| `-- src
| `-- image_func.cc
`-- math
|-- CMakeLists.txt
|-- include
| `-- math_util.h
`-- src
`-- math_util.cc
Roots CMakelists.txt
is below.根CMakelists.txt
如下。 Is it possible to set math and image parameters for app1 and app2?是否可以为 app1 和 app2 设置数学和图像参数? My actual project has many applications which uses multiple libraries.我的实际项目有许多使用多个库的应用程序。
cmake_minimum_required(VERSION 2.8)
add_subdirectory("./image")
add_subdirectory("./math")
add_subdirectory("./app1")
add_subdirectory("./app2")
With newer versions oft CMake (since 2.8.12) you can use target_link_libraries
and related functions to manage dependencies.对于较新版本的 CMake(自 2.8.12 起),您可以使用target_link_libraries
和相关函数来管理依赖项。 By specifying PUBLIC the includes and libraries are also applied to all targets using the library.通过指定 PUBLIC,包含和库也应用于使用库的所有目标。 This will reduce duplication to a minimum.这会将重复减少到最低限度。
For math and image you need to specify that the respective include directory is used and any libraries you might require.对于数学和图像,您需要指定使用相应的包含目录以及您可能需要的任何库。
math/CMakeLists.txt数学/CMakeLists.txt
add_library(math ...)
target_include_directories(math PUBLIC include ...)
target_link_libraries(math PUBLIC ...)
image/CMakeLists.txt图像/CMakeLists.txt
add_library(image ...)
target_include_directories(image PUBLIC include ...)
target_link_libraries(image PUBLIC ...)
app1/CMakeLists.txt app1/CMakeLists.txt
add_executabke(app1 ...)
target_link_libraries(app1 PUBLIC image math)
app2/CMakeLists.txt app2/CMakeLists.txt
add_executabke(app2 ...)
target_link_libraries(app2 PUBLIC image math)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.