英文:
Avoid relative path in Header file includes with Cmake
问题
以下是翻译好的内容:
我有一个结构如下的项目 -
app
|--src
| |---module
| | |---file1.cpp
| | |---file1.hpp
| |---file2.cpp
| |---file2.hpp
| |---main.cpp
|---CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
add_executable(testApp)
target_include_directories( testApp INTERFACE src)
target_sources(
src/file2.cpp
src/file2.cpp
src/module/file1.hpp
src/module/file1.cpp
src/main.cpp
)
file1.cpp
#include"../file2.hpp"
//....一些代码.....
问题: 在file1.cpp中,我包含了#include"../file2.hpp"
,给出了对file2.hpp的相对路径。我如何避免这样做??
我在CMakeLists.txt中包含了这个路径
target_include_directories( testApp INTERFACE src)
但当我使用#include "file2.hpp"
时,我得到一个"文件未找到"的错误。我如何将src文件夹包含到我的项目中,以便我可以直接使用头文件?
英文:
I have a project structured like below -
app
|--src
| |---module
| | |---file1.cpp
| | |---file1.hpp
| |---file2.cpp
| |---file2.hpp
| |---main.cpp
|---CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
add_executable(testApp)
target_include_directories( testApp INTERFACE src)
target_sources(
src/file2.cpp
src/file2.cpp
src/module/file1.hpp
src/module/file1.cpp
src/main.cpp
)
file1.cpp
#include"../file2.hpp"
//....some code.....
Question: In file1.cpp, I am including #include"../file2.hpp"
giving a relative path to file2.hpp. How do I avoid this ??
I am including this path to CMakeLists.txt
target_include_directories( testApp INTERFACE src)
But when I do #include "file2.hpp"
I get a file not found
error. How do I include the src
folder to my project so that I can use the headers file directly ??
答案1
得分: 2
以下是已翻译的内容:
"INTERFACE"、"PUBLIC"和"PRIVATE"关键词是必需的,用于指定以下参数的范围。 "PRIVATE"和"PUBLIC"项将填充
当使用"target_link_libraries()"指定目标依赖项时,CMake将从所有目标依赖项中读取此属性,以确定使用者的构建属性。
所以基本上,"INTERFACE"表示只有链接到给定目标的目标才会具有这些包含目录。当前目标构建不会受到影响。
只需将此更改为"PUBLIC":
target_include_directories(testApp PUBLIC src)
英文:
target_include_directories — CMake 3.27.0-rc4 Documentation
> The INTERFACE
, PUBLIC
and PRIVATE
keywords are required to
> specify the
> scope
> of the following arguments. PRIVATE
and PUBLIC
items will
> populate the
> [INCLUDE_DIRECTORIES
](https://cmake.org/cmake/help/latest/prop_tgt/INCLUDE_DIRECTORIES.html#prop_tgt:INCLUDE_DIRECTORIES
> "INCLUDE_DIRECTORIES") property of <target>
. PUBLIC
and
> INTERFACE
items will populate the [INTERFACE_INCLUDE_DIRECTORIES
](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_INCLUDE_DIRECTORIES.html#prop_tgt:INTERFACE_INCLUDE_DIRECTORIES
> "INTERFACE_INCLUDE_DIRECTORIES") property of <target>
. The
> following arguments specify include directories.
INTERFACE_INCLUDE_DIRECTORIES — CMake 3.27.0-rc4 Documentation
> When target dependencies are specified using
> [target_link_libraries()
](https://cmake.org/cmake/help/latest/command/target_link_libraries.html#command:target_link_libraries
> "target_link_libraries"), CMake will read this property from all
> target dependencies to determine the build properties of the consumer.
So basically INTERAFACE
means that only target which links to given target will have this include directories. Current target build will not be impacted.
Just change this to PUBLIC
:
target_include_directories( testApp PUBLIC src)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论