在 CMake 中列出 include_directories [英] Listing include_directories in CMake

查看:107
本文介绍了在 CMake 中列出 include_directories的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 cmake 构建,我在其中搜索一堆依赖项,即我有很多实例:

I have a cmake build in which I'm searching for a bunch of dependencies, i.e. I have many instances of:

FIND_PACKAGE(SomePackage)
if(SOMEPACKAGE_FOUND)
  include_directories(${SOMEPACKAGE_INCLUDE_DIR})
  link_libraries(${SOMEPACKAGE_LIBRARIES})
endif(SOMEPACKAGE_FOUND)

现在,我想添加一个自定义命令来构建一个预编译的头文件,但要做到这一点,我需要知道我的 include_directories 调用添加的所有路径.如何获取这些目录的列表(最好使用正确的 -I/path/to/directory 格式)以便将它们添加到我的自定义命令中?

Now, I want to add a custom command to build a precompiled header file, but to do this I need to know all of the paths added by my include_directories calls. How can I get a list of these directories (preferably with the proper -I/path/to/directory format) so that I can add them to my custom command?

推荐答案

可以使用 get_property 命令检索目录属性的值INCLUDE_DIRECTORIES

You can use the get_property command to retrieve the value of the directory property INCLUDE_DIRECTORIES

像这样:

get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
  message(STATUS "dir='${dir}'")
endforeach()

此目录属性的值仅跟踪先前在同一 CMakeLists 文件中出现的 include_directories 命令,或从父 CMakeLists 文件中先前出现的事件继承的 include_directories 命令.如果您的 find_package 和 include_directories 命令分散在许多子目录中,这将成为一个具有挑战性的问题.

The value of this directory property only tracks the include_directories commands that have occurred previously in the same CMakeLists file, or that have been inherited from previous occurrences in a parent CMakeLists file. If your find_package and include_directories commands are scattered about throughout many subdirectories, this becomes a challenging issue.

如果达到了这一点,您可以考虑使用自己的函数或宏覆盖 include_directories 命令,并自己跟踪传递给它的值.或者,在每次调用 include_directories 的同时,将它们简单地累积在全局属性或内部缓存变量中.

If you get to that point, you may consider overriding the include_directories command with your own function or macro and track the values passed to it yourself. Or, simply accumulate them in a global property or an internal cache variable alongside each call to include_directories.

请参阅此处的文档:

http://cmake.org/cmake/help/v2.8.8/cmake.html#command:get_property

http://cmake.org/cmake/help/v2.8.8/cmake.html#prop_dir:INCLUDE_DIRECTORIES

这篇关于在 CMake 中列出 include_directories的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆