避免在CMake中包含头文件时使用相对路径。

huangapple go评论72阅读模式
英文:

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"项将填充的"INCLUDE_DIRECTORIES"属性。 "PUBLIC"和"INTERFACE"项将填充的"INTERFACE_INCLUDE_DIRECTORIES"属性。以下参数指定包含目录。

当使用"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)

huangapple
  • 本文由 发表于 2023年7月4日 21:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定