【发布时间】:2020-11-09 00:13:49
【问题描述】:
我正在第一次使用 CMake 组合一个小型 c++ 项目。我目前的项目结构是
├── bch
│ ├── CMakeLists.txt
│ ├── gf
│ │ ├── CMakeLists.txt
│ │ ├── include
│ │ │ └── gf.h
│ │ └── src
│ │ └── gf.cpp
│ ├── include
│ │ └── bch.h
│ └── src
│ └── bch.cpp
├── bsc
│ ├── CMakeLists.txt
│ ├── include
│ │ └── bsc.h
│ └── src
│ └── bsc.cpp
├── CMakeLists.txt
├── .gitignore
└── main.cpp
目前我将gf 作为bch 的子目录。 bch/CMakeLists的内容是
cmake_minimum_required(VERSION 3.17)
project(bch VERSION 0.1.0)
# Targets
add_library(bch STATIC src/bch.cpp)
# Dependant
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/gf)
target_link_libraries(bch PUBLIC gf)
# Export to dependants
target_include_directories(bch PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
我想将gf CMake 项目放在bch 的目录路径之外。使用 add_subdirectory 命令时,这似乎不是受支持的结构,除非我遗漏了什么。一般来说,目前实现我将目录结构与依赖关系树解耦的目标的“最佳实践”是什么?
【问题讨论】:
-
"我想将
gfCMake 项目放在bch的目录路径之外。使用add_subdirectory命令时,这似乎不是受支持的结构" - 为什么你认为它不被支持?如果您收到错误add_subdirectory not given a binary directory,请参阅that question 以及有关解决方法的答案。 -
这是我选择的当前配置,直到我可以将
gf库打包到一个模块中。我将bch/build目录用于gf二进制文件,这似乎可以工作。我正在阅读的各种博客文章似乎暗示add_subdirectory建议用于树内紧密耦合的项目。