INTERFACE_SYSTEM_INCLUDE_DIRECTORIES目标属性为什么不会将包含目录添加到目标?

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

Why doesn't the INTERFACE_SYSTEM_INCLUDE_DIRECTORIES target property add include directories to the target?

问题

I have a slightly unusual setup where I need some header-only library to be included in a project. The headers are located within a sub-folder of the project (e.g. mylib/include). Now I wanted to create an imported interface library out of it - I cannot create a static, shared etc. library, since it is header only it needs to be an interface library.
但我需要为所有库的使用者指定包含目录。根据文档,我可以通过指定 INTERFACE_*IMPORTED_* 目标属性来实现这一点。因为我希望这个库被用作第三方库,使用系统包含文件。因此,我指定了:

add_library (mylib INTERFACE IMPORTED GLOBAL)
set_target_properties (mylib PROPERTIES
    INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/mylib/include
)

在项目的其他地方,我这样做:

target_link_libraries (runner PRIVATE mylib)

在配置期间,CMake 也接受了这个设置。但是当我尝试构建项目时,无法找到 mylib 的头文件 - CMake 没有将 mylib/include 添加到 runner 的包含目录中。

在生成的 compile_commands.json 中,mylib/include 文件夹不存在。
我漏掉了什么吗?我需要指定其他内容吗?

英文:

I have a slightly unusual setup where I need some header-only library to be included in a project. The headers are located within a sub-folder of the project (e.g. mylib/include). Now I wanted to create an imported interface library out of it - I cannot create a static, shared etc. library, since it is header only it needs to be an interface library.
But I need to specify the include directory for all consumers of the library. According to the documentation I can do this by specifying the INTERFACE_* and IMPORTED_* target properties. Since I want the library to be used as a 3rd party library, by using system includes. Therefore I specified:

add_library (mylib INTERFACE IMPORTED GLOBAL)
set_target_properties (mylib PROPERTIES
    INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/mylib/include
)

Somewhere else in the project I do:

target_link_libraries (runner PRIVATE mylib)

This is also accepted by CMake during configuration. But when I try to build the project, the header files of mylib cannot be found - CMake does not append mylib/include to the include directories of runner.

In the generated compile_commands.json the folder of mylib/include is not present.
Am I missing something? Do I need to specify something else?

答案1

得分: 1

这是INTERFACE_INCLUDE_DIRECTORIES属性,其中包含库的消费者中使用的包含目录。属性INTERFACE_SYSTEM_INCLUDE_DIRECTORIES仅用于将一些包含目录标记为系统目录:

> 项目也可以直接获取和设置此属性,但必须注意,将目录添加到此属性不会使这些目录在编译期间被使用。将目录添加到此属性会将目录标记为系统目录,否则将以非系统方式使用。

因此,您可以填充INTERFACE_INCLUDE_DIRECTORIESINTERFACE_SYSTEM_INCLUDE_DIRECTORIES属性...或仅填充第一个:IMPORTED目标的所有包含目录默认都被视为系统目录:

add_library (mylib INTERFACE IMPORTED GLOBAL)
set_target_properties (mylib PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/mylib/include
)
英文:

It is INTERFACE_INCLUDE_DIRECTORIES property which contains include directories for use in the consumers of a library. The property INTERFACE_SYSTEM_INCLUDE_DIRECTORIES is used only for mark some of include directories as system:

> Projects may also get and set the property directly, but must be aware that adding directories to this property does not make those directories used during compilation. Adding directories to this property marks directories as system directories which otherwise would be used in a non-system manner.

So, you may fill both of INTERFACE_INCLUDE_DIRECTORIES and INTERFACE_SYSTEM_INCLUDE_DIRECTORIES properties ... or fill only the first one: all include directories of an IMPORTED target are treated as system by default:

add_library (mylib INTERFACE IMPORTED GLOBAL)
set_target_properties (mylib PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/mylib/include
)

huangapple
  • 本文由 发表于 2023年4月17日 22:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036408.html
匿名

发表评论

匿名网友

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

确定