Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake executable with auto-generated sources

Tags:

c++

cmake

I want to make an executable from, for example, test_runner.cpp:

add_executable(myexe ${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp)

But this particular cpp file is itself auto-generated in a pre-build command:

add_custom_command(
    TARGET myexe PRE_BUILD
    COMMAND deps/cxxtest-4.4/bin/cxxtestgen --error-printer -o "${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp" src/My_test_suite.h
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

But now I can't generate new cmake build files because it complains about the missing source, which is indeed missing until pre-build.

like image 232
Jeff M Avatar asked Nov 28 '25 17:11

Jeff M


1 Answers

The crux of the issue is to apply the GENERATED property to "test_runner.cpp". This tells CMake not to check for its existence at configure time, since it gets created as part of the build process.

You can apply this property manually (e.g. using set_source_files_properties). However, the proper way to handle this is to use the other form of add_custom_command, i.e. add_custom_command(OUTPUT ...) rather than add_custom_command(TARGET ...).

If you specify "test_runner.cpp" as the output of the add_custom_command(OUTPUT ...) call, then any target consuming it (in this case "myexe") will cause the custom command to be invoked before that target is built.

So you really only need to change your code to something like:

set(TestRunner "${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp")
add_executable(myexe ${TestRunner})
add_custom_command(
    OUTPUT ${TestRunner}
    COMMAND deps/cxxtest-4.4/bin/cxxtestgen --error-printer -o "${TestRunner}" src/My_test_suite.h
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
like image 129
Fraser Avatar answered Dec 01 '25 07:12

Fraser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!