使用该get_cmake_property
函数,以下循环将打印出所有已定义的CMake变量及其值:
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
也可以将其嵌入便利函数中,该函数可以选择使用正则表达式仅打印名称匹配的变量子集
function(dump_cmake_variables)
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
if (ARGV0)
unset(MATCHED)
string(REGEX MATCH ${ARGV0} MATCHED ${_variableName})
if (NOT MATCHED)
continue()
endif()
endif()
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
endfunction()
要打印环境变量,请使用CMake的命令模式:
execute_process(COMMAND "${CMAKE_COMMAND}" "-E" "environment")