Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查CMake中的C程序输出_C++_C_Cmake - Fatal编程技术网

C++ 检查CMake中的C程序输出

C++ 检查CMake中的C程序输出,c++,c,cmake,C++,C,Cmake,是否可以对程序的输出进行平台检查 假设我想编译这个程序: #include <library.h> #include <iostream> int main() { std::cout << LIBRARY_MAGIC_VALUE << std::endl; return 0; } #包括 #包括 int main(){ std::cout使用命令进行此类检查: try_run(LIBRARY_MAGIC_VAL_RUN_RESU

是否可以对程序的输出进行平台检查

假设我想编译这个程序:

#include <library.h>
#include <iostream>
int main() {
    std::cout << LIBRARY_MAGIC_VALUE << std::endl;
    return 0;
}
#包括
#包括
int main(){
std::cout使用命令进行此类检查:

try_run(LIBRARY_MAGIC_VAL_RUN_RESULT
    LIBRARY_MAGIC_VAL_COMPILE_RESULT
    ${CMAKE_CURRENT_BINARY_DIR}/library_magic_val
    "test_library_magic_val.c"
    RUN_OUTPUT_VARIABLE LIBRARY_MAGIC_VAL_OUTPUT)

# Do not forget to check 'LIBRARY_MAGIC_VAL_RUN_RESULT' and
# 'LIBRARY_MAGIC_VAL_COMPILE_RESULT' for successfullness.

# Variable LIBRARY_MAGIC_VAL_OUTPUT now contains output of your test program.

可以按如下方式使用CMake内置命令:

TRY_RUN(
    # Name of variable to store the run result (process exit status; number) in:
    test_run_result

    # Name of variable to store the compile result (TRUE or FALSE) in:
    test_compile_result

    # Binary directory:
    ${CMAKE_CURRENT_BINARY_DIR}/

    # Source file to be compiled:
    ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp

    # Where to store the output produced during compilation:
    COMPILE_OUTPUT_VARIABLE test_compile_output

    # Where to store the output produced by running the compiled executable:
    RUN_OUTPUT_VARIABLE test_run_output)
这将尝试编译并运行给定的签入
test.cpp
。您仍然需要检查
try\u run
是否成功编译并运行了检查,并适当地处理输出。例如,您可以执行以下操作:

# Did compilation succeed and process return 0 (success)?
IF("${test_compile_result}" AND ("${test_run_result}" EQUAL 0))
    # Strip whitespace (such as the trailing newline from std::endl)
    # from the produced output:
    STRING(STRIP "${test_run_output}" test_run_output)
ELSE()
    # Error on failure and print error message:
    MESSAGE(FATAL_ERROR "Failed check!")
ENDIF()

CMake 3.0有一个“执行\进程”,我想不推荐的版本是“执行\程序”。你不必试图去做,你可以做一个返回库\魔法\值,并在“执行\进程”中设置返回变量然后使用该值进行检查?execute_进程不包括正确构建程序?如果我将该程序添加为目标,如何在配置步骤中指定需要该程序?正确,这更像是运行辅助程序来确定所需的值。您可以将其添加为依赖项。因此,VersionChecker首先,然后是AppUsingVersionChecker,每个都有自己的CMakeList.txt。谢谢,这解决了这个问题。还要注意,要指定包含路径,必须使用
CMAKE_标志
关键字,例如
try_run(run_RESULT COMPILE_RESULT binary source.c CMAKE_标志-DINCLUDE_目录=/path/to/foo run_OUTPUT_变量OUTVAR)
谢谢,try_run是一个好办法