在 CMake 中列出 include

编程入门 行业动态 更新时间:2024-10-26 02:35:10
本文介绍了在 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/cmake/help/v2.8.8/cmake.html#command:get_property

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

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

更多推荐

[db:关键词]

本文发布于:2023-03-31 05:10:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/796028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CMake   include

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!