在我的cmake文件中,我使用运行命令execute_process。我想检查一下是否失败。它不会打印任何内容stderr。
到目前为止,我一直在使用运行命令的bash脚本,然后使用来检查退出状态$? == 1。
有没有办法用cmake做类似的事情?
execute_process(COMMAND "runThis")
if("{$?}" EQUAL 1)
message( FATAL_ERROR "Bad exit status")
endif()
Run Code Online (Sandbox Code Playgroud)
我用cmake 3.12.1
cal*_*fir 10
The documentation indicates that RESULT_VARIABLE may be used to check the status of execute_process. Since it may contain either a string or an exit-code, both should be considered when checking for errors.
Here is an example putting that into practice:
# good
execute_process(
COMMAND cmake --version
RESULT_VARIABLE STATUS
OUTPUT_VARIABLE OUTPUT1
ERROR_QUIET )
if(STATUS AND NOT STATUS EQUAL 0)
message(STATUS "FAILED: ${STATUS}")
else()
message(STATUS "SUCCESS: ${OUTPUT1}")
endif()
# nonzero exit code
execute_process(
COMMAND cmake -G xxxx
RESULT_VARIABLE STATUS
OUTPUT_VARIABLE OUTPUT2
ERROR_QUIET )
if(STATUS AND NOT STATUS EQUAL 0)
message(STATUS "FAILED: ${STATUS}")
else()
message(STATUS "SUCCESS: ${OUTPUT2}")
endif()
# bad command
execute_process(
COMMAND xxxx
RESULT_VARIABLE STATUS
OUTPUT_VARIABLE OUTPUT3
ERROR_QUIET )
if(STATUS AND NOT STATUS EQUAL 0)
message(STATUS "FAILED: ${STATUS}")
else()
message(STATUS "SUCCESS: ${OUTPUT3}")
endif()
Run Code Online (Sandbox Code Playgroud)
output:
SUCCESS: cmake version 3.18.0-rc3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
FAILED: 1
FAILED: The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
cmakeexecute_process,出错时退出
从 cmake 3.19 开始,我们有选项COMMAND_ERROR_IS_FATAL
execute_process(COMMAND echo hello COMMAND_ERROR_IS_FATAL ANY)
Run Code Online (Sandbox Code Playgroud)
您可以使用调用RESULT_VARIABLE选项找到执行过程的退出状态execute_process。从选项的文档中:
该变量将被设置为包含运行进程的结果。这将是最后一个子代的整数返回码或描述错误情况的字符串。
例:
execute_process(COMMAND "runThis" RESULT_VARIABLE ret)
if(ret EQUAL "1")
message( FATAL_ERROR "Bad exit status")
endif()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1350 次 |
| 最近记录: |