简体   繁体   English

如何从另一个include目录中的头文件中访问include目录中的头文件?

[英]How to access headers in an include directory from headers in another include directory?

I am trying to include multiple directories using target_include_directories in one target, but headers in one include directory cannot access headers in another include directory. 我试图在一个目标中使用target_include_directories包含多个目录,但是一个include目录中的标头无法访问另一个include目录中的标头。 How do I solve this problem? 我该如何解决这个问题?

I tried putting the header files into add_library, but it doesn't help. 我尝试将头文件放入add_library,但它没有帮助。 I have also searched here for two hours just now, but I cannot seem to find anything about it. 我刚刚在这里搜索了两个小时,但我似乎无法找到任何相关信息。

I am using VC++ by the way. 我顺便使用VC ++。

The following project is simplified, the real project is here . 以下项目已简化,真正的项目就在这里

Project structure: 项目结构:

project
|-- CMakeLists.txt
|-- include
|   |-- project_header.h
|-- subproject
|   |-- CMakeLists.txt
|   |-- include
|   |   |-- subproject_header.h
|   |-- ...
|-- ...

project/CMakeLists.txt : project/CMakeLists.txt

project("example")
add_subdirectory("subproject")

project/subproject/CMakeLists.txt : project/subproject/CMakeLists.txt

add_executable(example "${PROJECT_SOURCE_DIR}/subproject/source/main.hpp")
set_target_properties(example PUBLIC_HEADER "${PROJECT_SOURCE_DIR}/subproject/include/subproject_header.h" PRIVATE_HEADER "${PROJECT_SOURCE_DIR}/include/project_header.h"
target_include_directories(example PUBLIC "include" PRIVATE "${PROJECT_SOURCE_DIR}/include")

The problem is when I type this into project/subproject/include/subproject_header.h : 问题是当我将其键入project/subproject/include/subproject_header.h

...
#include "project_header.h"
...

This will produce an error: fatal error C1083: Cannot open include file: 'project_header.h': No such file or directory 这将产生错误: fatal error C1083: Cannot open include file: 'project_header.h': No such file or directory

How do I solve this problem? 我该如何解决这个问题?

Edit: 编辑:

The problem seems to have fixed itself after using .. instead of ${PROJECT_SOURCE_DIR} . 使用..而不是${PROJECT_SOURCE_DIR}后,问题似乎已经解决了。 Then I change it back to ${PROJECT_SOURCE_DIR} and it still works. 然后我将其更改回${PROJECT_SOURCE_DIR} ,它仍然有效。 Is this related to caching? 这与缓存有关吗?

Let's say my project is like this: 假设我的项目是这样的:

project
|---include
|   |---common.h
|---library
|---executable

project/CMakeLists.txt : project/CMakeLists.txt

project(example)
add_subdirectory(library)
add_subdirectory(executable)

project/library/CMakeLists.txt : project/library/CMakeLists.txt

add_library(lib ...)
target_include_directories(lib PRIVATE "../include")

project/executable/CMakeLists.txt : project/executable/CMakeLists.txt

add_executable(exe ...)
target_link_libraries(exe lib)

If I try to access common.h from files in executable , then the compiler will complain about the error mentioned above. 如果我尝试从executable文件中访问common.h ,那么编译器会抱怨上面提到的错误。 This is caused by that PRIVATE include headers will not be included in its dependants. 这是因为PRIVATE include标头不会包含在其依赖项中。

The solution is to add this to project/projectB/CMakeLists.txt : target_include_directories(exe PRIVATE "../include") 解决方案是将其添加到project/projectB/CMakeLists.txttarget_include_directories(exe PRIVATE "../include")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM