如何通过 cmake 共享子目录中的标头和库
How do I share headers and libraries in subdirectory by cmake?
我想将我的标头和库用作 app1 和 app2 的公共库。我的项目树在下面。 image/
和 math/
是 app1 和 app2 使用的库目录。在这种情况下,我是否应该为 app1 和 app2 下的CmakeLists.txt
设置相同的设置?我当然知道它有效,但是有没有更聪明的方法来设置公共库?
|-- 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
根CMakelists.txt
如下。是否可以为 app1 和 app2 设置数学和图像参数?我的实际项目有许多使用多个库的应用程序。
cmake_minimum_required(VERSION 2.8)
add_subdirectory("./image")
add_subdirectory("./math")
add_subdirectory("./app1")
add_subdirectory("./app2")
使用较新版本的 cmake(自 2.8.12 起),您可以使用target_link_libraries
和相关函数来管理依赖项。通过指定 PUBLIC,包含和库也将应用于使用该库的所有目标。这将把重复减少到最低限度。
对于数学和图像,您需要指定使用相应的包含目录以及您可能需要的任何库。
math/CMakeLists.txt
add_library(math ...)
target_include_directories(math PUBLIC include ...)
target_link_libraries(math PUBLIC ...)
图像/CMakeLists.txt
add_library(image ...)
target_include_directories(image PUBLIC include ...)
target_link_libraries(image PUBLIC ...)
app1/CMakeLists.txt
add_executabke(app1 ...)
target_link_libraries(app1 PUBLIC image math)
app2/CMakeLists.txt
add_executabke(app2 ...)
target_link_libraries(app2 PUBLIC image math)
相关文章:
- 通过 CMake 添加子项目
- 通过 cmake 从C++扩展构建 Python 子模块
- 通过cMake使用具有自定义文件扩展名的共享库
- 如何通过 CMake 使用他们的 SHA256 哈希下载提升?
- 通过CMake在没有Qt Creator的情况下构建Qt5 Quick项目
- 如何在 MSYS2 中通过 CMake 构建 c++ Qt5 应用程序
- 通过 CMake 将外部库包含在 CLion 项目中
- 无法通过 CMake 链接 Boost 1.63.0
- 链接QT5库会自动将额外的FPIC标志传递给NVCC编译器,并通过cmake Cause错误
- 如何正确设置Windows路径以通过CMAKE轻松识别
- 包括通过CMAKE工具链正确版本的STL标题
- 通过CMAKE在Visual Studio中设置优化设置
- 通过cmake添加外部库(例如,特征)
- 嵌入库及其's包括通过CMake
- 通过 CMake 2.8 静态链接 Linux C++应用程序
- 如何在linux上通过cmake链接googleprotobuf库
- 如何通过cmake链接ffmpeg库
- 通过CMake实施Qt项目
- 通过CMake定义预处理器宏
- 通过CMake构建的ios上Qt应用程序的运行时错误