在 CMake 中找不到带有'include_directories'的标头

Cannot find a header with 'include_directories' in CMake

本文关键字:directories include CMake 找不到      更新时间:2023-10-16

这是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中评论包含的内容,则可以使用。另外,如果我在file1.cpp中包括file2.h,它的工作原理是,主要问题是我在其他子目录中的另一个标头文件中包含一个子目录中的标题文件,其中一个包含在主文件中。

任何帮助将不胜感激。

命令 include_directories仅在 local 范围内影响:在 FolderA中发出它会影响仅在FolderA库的汇编时。您需要在编译main.cpp的顶级CMakeLists.txt中发出该命令。

另外,您可以在 FolderA中使用 public 选项使用命令target_include_directories:

target_include_directories(FolderA PUBLIC ../FolderB)

因此,包括目录将传播到与该库链接的任何目标。