C++ CMake 中的 include_directories 和 target_include_directories 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31969547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between include_directories and target_include_directories in CMake?
提问by Ujjwal Aryan
I have a directory structure for my C++ code which goes like this :
我的 C++ 代码有一个目录结构,如下所示:
|
|->include
|->src
I am writing a CMakeLists.txt file for my code. I want to understand the difference between include_directories
and target_include_directories
in CMake
.
我正在为我的代码编写 CMakeLists.txt 文件。我想了解include_directories
和target_include_directories
in之间的区别CMake
。
What is the difference between their usage and in order to add my include file path which one should I be using?
它们的用法与为了添加我的包含文件路径我应该使用哪一个有什么区别?
回答by Angew is no longer proud of SO
include_directories(x/y)
affects directory scope. All targets in this CMakeList, as well as those in all subdirectories added after the point of its call, will have the path x/y
added to their include path.
include_directories(x/y)
影响目录范围。此 CMakeList 中的所有目标,以及在其调用点之后添加的所有子目录中的目标,都会将路径x/y
添加到它们的包含路径中。
target_include_directories(t x/y)
has target scope—it adds x/y
to the include path for target t
.
target_include_directories(t x/y)
具有目标范围——它添加x/y
到目标的包含路径t
。
You want the former one if all of your targets use the include directories in question. You want the latter one if the path is specific to a target, or if you want finer control of the path's visibility. The latter comes from the fact that target_include_directories()
supports the PRIVATE
, PUBLIC
, and INTERFACE
qualifiers.
如果您的所有目标都使用相关的包含目录,则您需要前者。如果路径特定于目标,或者您想要更好地控制路径的可见性,则您需要后者。后者来自于这样的事实target_include_directories()
支持PRIVATE
,PUBLIC
和INTERFACE
预选赛。
回答by Antonio
Beside what Angew's answercorrectly says, another very important difference between include_directories
and target_include_directories
is that, when used with PUBLIC
or INTERFACE
, the latter populate the INTERFACE_INCLUDE_DIRECTORIES
property of the target. This property is useful when another target uses target_link_libraries
to link to the original target, as the linking target will have automatically those include directories added. See example.
除了Angew 的正确回答之外,include_directories
and之间的另一个非常重要的区别target_include_directories
是,当与PUBLIC
or 一起使用时INTERFACE
,后者填充INTERFACE_INCLUDE_DIRECTORIES
目标的属性。当另一个目标用于target_link_libraries
链接到原始目标时,此属性很有用,因为链接目标将自动添加那些包含目录。参见示例。
This important feature is pretty well hidden in the documentation: target_include_directoriesmention populating INTERFACE_INCLUDE_DIRECTORIES
, whose documentationsays:
这个重要的特性很好地隐藏在文档中:target_include_directories提到 populating INTERFACE_INCLUDE_DIRECTORIES
,其文档说:
When target dependencies are specified using target_link_libraries(), CMake will read this property from all target dependencies to determine the build properties of the consumer.
当使用target_link_libraries()指定目标依赖项时,CMake 将从所有目标依赖项中读取此属性以确定使用者的构建属性。
回答by Nick.Rhan
As @Angew said, the very difference is :
正如@Angew 所说,非常不同的是:
1, include_directories() is accessible for all the files in the source-tree 2, target_include_directories() is-only accessible for a specific target when compile.
1、include_directories() 可被源树中的所有文件访问 2、target_include_directories() 仅在编译时可被特定目标访问。