我想知道是否有办法在CMake中打印出所有可访问的变量。我对CMake变量不感兴趣 - 就像在--help-variables选项中一样。我在谈论我定义的变量,或者包含脚本定义的变量。
我目前正在加入
INCLUDE (${CMAKE_ROOT}/Modules/CMakeBackwardCompatibilityCXX.cmake)
我希望我能打印出这里的所有变量,而不是必须浏览所有文件并阅读可用内容 - 我可能会发现一些我不知道的变量可能有用。帮助学习和帮助是好的。发现。它严格用于调试/开发。
这类似于 Print all local variables accessible to the current scope in Lua 中的问题,但是对于CMake来说!
有没有人这样做过?
答案 0 :(得分:308)
使用get_cmake_property函数,以下循环将打印出定义的所有CMake变量及其值:
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
要打印环境变量,请使用CMake的command mode:
execute_process(COMMAND "${CMAKE_COMMAND}" "-E" "environment")
答案 1 :(得分:153)
另一种方法是简单地使用:
cmake -LAH
来自manpage:
-L[A][H]
列出非高级缓存变量。
列表缓存变量将运行CMake并列出CMake缓存中未标记为INTERNAL或ADVANCED的所有变量。这将有效地显示当前的CMake设置[...]。
如果指定了A,那么它也会显示高级变量。
如果指定了H,它还将显示每个变量的帮助。
答案 2 :(得分:6)
ccmake
是交互式检查缓存变量(option(
或set( CACHE
的良好互动选项:
sudo apt-get install cmake-curses-gui
mkdir build
cd build
cmake ..
ccmake ..
答案 3 :(得分:0)
基于@sakra
/**
* send_body___receive_response.js
*
* Can of-course be in <script> tags in HTML or PHP
*/
async function postData( url='', data={ } ) {
// *starred options in comments are default values
const response = await fetch(
url,
{
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "same-origin", // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json", // sent request
"Accept": "application/json" // expected data sent back
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify( data ), // body data type must match "Content-Type" header
},
);
return response.json( ); // parses JSON response into native JavaScript objects
}
const data = {
'key1': 'value1',
'key2': 'value2'
};
postData( 'receive_body___send_response.php', JSON.stringify( data ) )
.then( response => {
// Manipulate response here
console.log( "response: ", response ); // JSON data parsed by `data.json()` call
// In this case where I send entire $decoded from PHP you could arbitrarily use this
console.log( "response.data: ", JSON.parse( response.data ) );
} );
变量名称区分大小写
如果您对Boost感兴趣,那就是/**
* receive_body___send_response.php
*/
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
// Set initial response
$response = [
'value' => 0,
'error' => 'Nothing happened',
'data' => null,
];
if ($contentType === "application/json") {
// Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
// $decoded can be used the same as you would use $_POST in $.ajax
$decoded = json_decode($content, true);
if(! is_array($decoded)) {
// NOTE: Sometimes for some reason I have to add the next line as well
/* $decoded = json_decode($decoded, true); */
// Do something with received data and include it in reponse
/* perhaps database manipulation here */
$response['data'] = $decoded;
// Success
$response['value'] = 1;
$response['error'] = null;
} else {
// The JSON is invalid.
$response['error'] = 'Received JSON is improperly formatted';
}
} else {
// Content-Type is incorrect
$response['error'] = 'Content-Type is not set as "application/json"';
}
// echo response for fetch API
echo json_encode($response);
而不是function(dump_cmake_variables)
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
if (ARGV0)
unset(MATCHED)
#case sensitive match
# string(REGEX MATCH ${ARGV0} MATCHED ${_variableName})
#
#case insenstitive match
string( TOLOWER "${ARGV0}" ARGV0_lower )
string( TOLOWER "${_variableName}" _variableName_lower )
string(REGEX MATCH ${ARGV0_lower} MATCHED ${_variableName_lower})
if (NOT MATCHED)
continue()
endif()
endif()
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
endfunction()
dump_cmake_variables("^Boost")
,它是Boost_INCLUDE_DIRS
而不是BOOST_INCLUDE_DIRS
,并且错误地我使用了BOOST_LIBRARIES而不是Boost_LIBRARIES, https://cmake.org/cmake/help/v3.0/module/FindBoost.html,是增强效果的更好示例:
Boost_LIBRARIES
答案 4 :(得分:0)
查看所有cmake内部变量的另一种方法是通过使用--trace-expand
选项执行cmake。
这将使您跟踪所有已执行的.cmake文件以及在每一行上设置的变量。
答案 5 :(得分:0)
您可以使用 message :
message([STATUS] "SUB_SOURCES : ${SUB_SOURCES}")
答案 6 :(得分:0)
当前的所有答案都不允许我看到项目子目录中的变量。这是一个解决方案:
function(print_directory_variables dir)
# Dump variables:
get_property(_variableNames DIRECTORY ${dir} PROPERTY VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
get_directory_property(_variableValue DIRECTORY ${dir} DEFINITION ${_variableName})
message(STATUS "DIR ${dir}: ${_variableName}=${_variableValue}")
endforeach()
endfunction(print_directory_variables)
# for example
print_directory_variables(.)
print_directory_variables(ui/qt)