c++ - CMake 使用 GLOB 结果写入头文件

标签 c++ cmake

我有一个 GLOB 表达式,例如:

file( GLOB HEADER_FILES path/*.h )

并希望生成一个头文件,其中包含每个 HEADER_FILES 条目的定义。例如,如果文件名为“path/my_class.h”,那么宏应该是:

#define PATH_MY_CLASS_FILE /full/path/to/path/my_class.h

这可以使用 bash 轻松完成,但我们也针对 windows 和 visual studio。有没有一种纯粹的 CMAKE 方法来完成这项工作?还是我必须编写一个快速的 C++ 程序并制作自定义依赖项/目标来生成文件?


根据我想出的答案:

file(WRITE headers.h "//Generated headers include\n")
foreach(header_path ${APP_SHARED_MOC_HEADERS})
    # drop full name for macro name
    string(REPLACE "${INCLUDE_DIR}/" "" header "${header_path}")
    # drop extension
    string(REPLACE ".h" "" header "${header}")

    string(REPLACE "/" "_" header "${header}")
    string(TOUPPER "${header}" define_name)

    file(APPEND headers.h "#define PATH_${define_name} ${header_path}\n")
endforeach()

最佳答案

尝试这样的事情:

file(GLOB HEADER_FILES path/*.h)

foreach(header ${HEADERS_FILES})
  # here replace / for linux and \ for windows
  string(REPLACE "/" "_" header_path header) 
  string(TOUPPER header_path final_header_path)
  file(APPEND headers_defines_file define 
    PATH_${final_header_path} ${CMAKE_CURRENT_SOURCE_DIR}/header)
endforeach(header)

关于c++ - CMake 使用 GLOB 结果写入头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5879805/

相关文章:

c++ - 使用 CMake 设置 C++ 目标文件输出位置

cmake - 指定 CTestTestfile.cmake 的目标

c++11 - CMake: ‘sqrtf’ 不是 ‘std’ 的成员

c++ - 如何在 CMake 中以可移植的方式获取 libstdcxx 隐式链接目录

c++ - rand_r 不在范围内,gcc mingw 在 Windows 上的 cygwin 下

CMake:使用自定义工具输出生成标题

c++ - 如何创建用户定义大小但没有预定义值的 vector ?

c++ - 预测 std::unordered_set 或 std::unordered_map 的调整大小/重新散列

c++ - 确定不打开文件的确切原因

android - 是否可以在 Flutter 应用程序中使用 C++ 进行网络编程?