英文:
How can I list the PRIVATE, PUBLIC and INTERFACE include directories of a target?
问题
这个问题:
https://stackoverflow.com/questions/6902149/listing-include-directories-in-cmake
教我们如何列出CMake目录的包含目录。如何在目标上做到这一点,同时区分PUBLIC、PRIVATE和INTERFACE目录?
英文:
This question:
https://stackoverflow.com/questions/6902149/listing-include-directories-in-cmake
teaches us how to list include directories for a CMake directory. How do we do that for a target, and separating the PUBLIC, PRIVATE and INTERFACE directories?
答案1
得分: 2
以下是翻译好的部分:
现在,相同的 getter 用于目录和目标,只是语法略有不同:
get_property(dirs1 TARGET my_target PROPERTY INCLUDE_DIRECTORIES)
现在,这将获取我们的两组包含目录的并集:PUBLIC
和 PRIVATE
,但不包括 INTERFACE
。我们还有:
get_property(dirs2 TARGET my_target PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
现在:
PUBLIC
目录是 dirs1 和 dirs2 的交集 (dirs1 ∩ dirs2)。PRIVATE
目录是 dirs1 减去 dirs2 (dirs1 ∖ dirs2)。INTERFACE
目录是 dirs2 减去 dirs1 (dirs2 ∖ dirs1)。
如你在链接的问题中所描述的,一旦你有了目录列表,你可以这样打印它们出来:
foreach(dir ${dirs2})
message(STATUS "my_target 的接口包含目录: ${dir}")
endforeach()
如果你需要实际生成这三个列表,请参考此问题来了解如何执行并集、交集和差异操作。
英文:
Well, the same getter works for directories and targets, just with slightly different syntax:
get_property(dirs1 TARGET my_target PROPERTY INCLUDE_DIRECTORIES)
Now, this will get us the union of two sets of include directories: PUBLIC
and PRIVATE
, but not INTERFACE
. We also get:
get_property(dirs2 TARGET my_target PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
Now:
- The
PUBLIC
directories are dirs1 intersection-with dirs2 (dirs1 ∩ dirs2). - The
PRIVATE
directories are dirs1 set-minus dirs2 (dirs1 ∖ dirs2). - The
INTERFACE
directories are dirs2 set-minus dirs1 (dirs2 ∖ dirs1).
As described in the question you linked to, once you have your list of directories, you can print it out like so:
foreach(dir ${dirs2})
message(STATUS "Interface include directory for my_target: ${dir}")
endforeach()
If you need to actually, concretely, generate the three lists - see this question about how to perform union, intersection and difference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论