本文介绍了catch返回值在CMake add_custom_command?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何获取 add_custom_command ?调用的命令的返回值 像这样, 宏(mac param1) execute_process(COMMAND process $ {param1} RESULT_VARIABLE res_var) if($ {res_var} .... endmacro(mac) add_custom_command(COMMAND mac(param1)) $ b 宏(mac) endmacro() add_custom_command(COMMAND mac()) 建立时,sh会抱怨: / bin / sh: 或者,如果我不使用宏, code> add_custom_command 本身: / bin / sh:1:语法错误:word unexpected(expecting)) 我猜想 add_custom_command 不希望传递宏或内置函数。但是,如何从 add_custom_command 中的命令获取返回值?或者,更具体地说,我如何捕获 add_custom_command 中的命令失败?解决方案由add_custom_command运行的命令在构建时间之前不会运行,因此您无法捕获它们在CMake的配置/生成步骤期间失败。 一个自定义命令失败,那么任何取决于该自定义命令的输出的东西都会被避免,因为make在此之后停止,或者如果make在错误之后继续运行,它最终将在其结尾返回一个错误, 失败的构建消息。 您可以编写一个等效的脚本作为自定义命令运行,然后在该脚本中,在某些类型的错误后做一些事情。如果在-P脚本处理模式下使用cmake,您可以创建一个调用execute_process并分析返回值的跨平台脚本。 例如: configure_file( $ {CMAKE_CURRENT_SOURCE_DIR} /script.cmake.in $ {CMAKE_CURRENT_BINARY_DIR} /script.cmake COPYONLY ) add_custom_command(COMMAND $ {CMAKE_COMMAND} -P $ {CMAKE_CURRENT_BINARY_DIR} /script.cmake ) 然后在script.cmake.in中: execute_process (COMMAND进程param1 RESULT_VARIABLE res_var) if(NOT$ {res_var}STREQUAL0)#关于失败的进程调用执行某些操作... message(FATAL_ERROR process failed res_var ='$ {res_var}') endif() how do I grab the return value of command invoked by add_custom_command?I thought I could do something like this,macro(mac param1) execute_process(COMMAND process ${param1} RESULT_VARIABLE res_var) if(${res_var} ....endmacro(mac)add_custom_command(COMMAND mac(param1))but that won't work. I found out that even a plainmacro(mac)endmacro()add_custom_command(COMMAND mac())does not work. Upon building, sh complains:/bin/sh: 1: Syntax error: end of file unexpectedor, if I do not use the macro but call execute_process in add_custom_command itself:/bin/sh: 1: Syntax error: word unexpected (expecting ")")I guess that add_custom_command does not expect macros or built-in functions to be passed. However, how can I get the return value from the command in add_custom_command? Or, less specifically, how can I catch that the command in add_custom_command failed? 解决方案 The commands run by add_custom_command do not run until build time, so you can't "catch" that they failed during CMake's configure/generate steps.If a custom command fails, then anything that depends on the output of that custom command will either be avoided, because make stops after that, or if make keeps going after errors, it will eventually return an error at its conclusion, and give some sort of "failed build" message.You can always write an equivalent script that runs as a custom command, and then in that script, do something after certain types of errors. If you use cmake in -P script processing mode, you can make a cross platform script that calls execute_process and analyzes the return value.For example:configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/script.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/script.cmake COPYONLY)add_custom_command(COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/script.cmake)And then in script.cmake.in:execute_process(COMMAND process param1 RESULT_VARIABLE res_var)if(NOT "${res_var}" STREQUAL "0") # do something here about the failed "process" call... message(FATAL_ERROR "process failed res_var='${res_var}'")endif() 这篇关于catch返回值在CMake add_custom_command?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-12 05:52