CMake not redirecting stderr with execute_process

2019-06-08 01:59发布

问题:

I'm trying to redirect stdout and stderr to the same file using CMake. I'm using the execute_process option in CMake with the ERROR_FILE and OUTPUT_FILE option specified.

I'm successfully capturing the output, but the error is not there. What am I doing wrong?

File CMakeLists.txt

add_test(NAME test${ID}
    COMMAND ${CMAKE_COMMAND}
    -DEXE=../examples/test${exampleID}
    -DID=${ID}
    -DARGS=${args}
    -P ${CMAKE_CURRENT_SOURCE_DIR}/Tester.cmake
)

File Tester.cmake

separate_arguments( ARGS )
# Run the test
execute_process( 
    COMMAND "${EXE}" ${ARGS}
    ERROR_FILE test${ID}.out
    OUTPUT_FILE test${ID}.out
)

回答1:

Specifying the same file for both OUTPUT_FILE and ERROR_FILE has only recently been added in CMake 3.3. See release notes.

As a work-around for earlier versions, use the options OUTPUT_VARIABLE and ERROR_VARIABLE with the same variable and then write the contents of the variable to the file, e.g.:

execute_process( 
    COMMAND "${EXE}" ${ARGS}
    ERROR_VARIABLE _testOut
    OUTPUT_VARIABLE _testOut
)
file (WRITE "test${ID}.out" "${_testOut}")