这是c ++项目的文件夹层次结构:
1. FolderA
1. file1.h/cpp
2. CMakeLists.txt (CmakeA)
2. FolderB
1. file2.h/cpp
2. CMakeLists.txt (CmakeB)
3. main.cpp
4. CMakeLists.txt (CmakeAll)
CmakeA:
file(GLOB FolderA_FILES *.cpp *.h *.hpp)
# add component
add_library(FolderA ${FolderA_FILES})
include_directories(../FolderB)
target_link_libraries(FolderA FolderB)
CMakeB:
file(GLOB FolderB_FILES *.cpp *.h *.hpp)
# add component
add_library(FolderB ${FolderB_FILES})
CMakeAll:
cmake_minimum_required(VERSION 2.8)
#add smirk dependency
set(Smirk_DIR /usr/local CACHE PATH "Directory where Smirk has been installed (e.g. /usr/local).")
include(${Smirk_DIR}/cmake/smirk.cmake)
smirk_project(operatingTableProject)
file(GLOB smirk_operatingTableProject_FILES *.cpp *.h *.hpp)
add_executable(smirk_operatingTableProject ${smirk_operatingTableProject_FILES})
# install directives
install(
TARGETS smirk_operatingTableProject
RUNTIME DESTINATION bin
)
#add FolderB dependency
add_subdirectory(FolderB)
target_link_libraries(smirk_operatingTableProject FolderB)
#add FolderA dependency
add_subdirectory(FolderA)
target_link_libraries(smirk_operatingTableProject FolderA)
在file1.h
我包括file2.h
。问题是我在main.cpp
FolderA/file1.h
中包含的任何地方我遇到了汇编问题:
file2.h: No such file or directory (in file1.h)
但是,如果我在main.cpp
中注释掉包含内容,那就可以了。此外,如果我在file2.h
中包含file1.cpp
,那么主要问题是我在其他子目录的另一个头文件中包含来自子目录的头文件,其中一个包含在主文件中。
任何帮助都将不胜感激。
答案 0 :(得分:8)
命令include_directories
仅影响本地范围:在FolderA
中发布,它会影响FolderA
库编译的 。您需要在顶级CMakeLists.txt
中为编译main.cpp
发出该命令。
或者,您可以在FolderA
中使用命令target_include_directories和 PUBLIC 选项:
target_include_directories(FolderA PUBLIC ../FolderB)
所以include目录将传播到与该库链接的任何目标。