【发布时间】:2017-02-19 00:14:14
【问题描述】:
TL;DR
使用 CMake,我如何将子目录包含到库中,以便在不引用它们所在目录的情况下包含它们?
结束 TL;DR
为了尽量简明扼要,并以更高层次的想法说明什么和如何,我删除了所有我认为不必要的细节。如果需要,我会进行编辑。因此,这是我的项目结构的简要概要。
ParentDir
--src
----source.cpp
----source.h
----entities_dir
------entity.cpp
------entity.h
------CMakeLists.txt
----CMakeLists.txt
--CMakeLists.txt
--main.cpp
就目前而言,我在 src 目录中有一个由 CMakeLists 定义的库。因此,我可以通过#include 将src 文件包含在main 中,与#include "src/file.h" 相对,我希望能够对src 子目录中存在的头文件执行相同的操作。
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(Project)
add_executable(Project ${SOURCE_FILES} main.cpp)
include_directories(src)
add_subdirectory(src)
target_link_libraries(Project Library) # Engine Libraries
src/CMakeLists.txt
file(GLOB SOURCE_FILES *.cpp)
file(GLOB HEADER_FILES *.h)
add_library(Library STATIC ${SOURCE_FILES} ${HEADER_FILES})
main.cpp
#include <source.h> // this works
#include <entity.h> // this does not work but I want it to
#include <entities/entity.h> // this works but I don't want this
int main() {}
我不确定该怎么做。我尝试了 GLOB_RECURSE、add_subdirectory(entities) 等。我还尝试在 src/entities/CMakeLists.txt 中创建一个名为 Entities 的库,并将其与 link_libraries 链接。这些都没有成功。完成此操作的正确方法是什么,因为我认为我可能完全错误地处理了这个问题。
【问题讨论】:
-
您需要在编译器头文件搜索路径中使用该路径,这是通过 include_directories() 调用实现的。您可以将现有的 include_directories(src) 调用修改为 include_directories(src src/entities)。我不知道如何进行递归,我也不推荐它。
-
这样就行了。不过,这似乎在挑战我对您应该如何使用 cmake 的理解。我已经看到在每个目录中都有 cmakelists 是司空见惯的。由于在这种情况下,src cmakelists 正在创建实际的库,所以父 cmake 不应该只需要引用 src cmake 而不需要了解 src 子目录吗?
-
@qexyn 如果你想留下答案,我会接受。