我有一个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()
答案 0 :(得分:2)
尝试这样的事情:
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)