具有自动生成的源的Cmake可执行文件 [英] Cmake executable with auto-generated sources
问题描述
我想从例如test_runner.cpp生成可执行文件:
I want to make an executable from, for example, test_runner.cpp:
add_executable(myexe ${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp)
但是这个特殊的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}"
)
但是现在我无法生成新的cmake构建文件,因为它抱怨缺少源,实际上在进行预构建之前就一直没有。
But now I can't generate new cmake build files because it complains about the missing source, which is indeed missing until pre-build.
推荐答案
问题的症结在于应用 GENERATED
属性以进行测试_runner.cpp。这告诉CMake不要在配置时检查它的存在,因为它是在构建过程中创建的。
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.
您可以手动应用此属性(例如,使用 set_source_files_properties
)。但是,解决此问题的正确方法是使用 add_custom_command
,即 add_custom_command(OUTPUT ...)
而不是 add_custom_command(TARGET ...)
。
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 ...)
.
如果指定 test_runner.cpp 作为 add_custom_command(OUTPUT ...)
调用的输出,那么使用它的任何目标(在本例中为 myexe)都会导致在调用该自定义命令之前
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}"
)
这篇关于具有自动生成的源的Cmake可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!