C++ 在 CMake 中添加自定义构建步骤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18427877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:58:00  来源:igfitidea点击:

Add custom build step in CMake

c++c++11buildcmakecustom-build-step

提问by David Bulczak

I'm trying to add a custom build step in CMake that generates some files. I haven't found a description how it works.

我正在尝试在 CMake 中添加一个生成一些文件的自定义构建步骤。我还没有找到它是如何工作的描述。

I have an project where source, header & implementation files have to be generated by ODB for C++. ODB takes class headers as arguments and generates source files that I want to use in my project.

我有一个项目,其中必须由 ODB for C++ 生成源文件、头文件和实现文件。ODB 将类头作为参数并生成我想在我的项目中使用的源文件。

Right now I have the following command in my CMakeLists.txt:

现在我的 CMakeLists.txt 中有以下命令:

add_custom_command(TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND odb -o /home/david/dev/ --std c++11 -I/home/david/dev/ -d sqlite --generate-    query --generate-schema ${PROMOTER_LIB_PREFIX}/entities/person.hpp
    DEPENDS ${PROJECT_NAME}
    VERBATIM
)

For a file person.hppODB should generate person-odb.hxx, person-odb.cxx, person-odb.ixxbut the CMake command I''ve used doesn't generate anything. In a terminal this command works fine.

对于文件person.hppODB 应该生成person-odb.hxx, person-odb.cxxperson-odb.ixx但我使用的 CMake 命令不会生成任何内容。在终端中,此命令工作正常。

What am I doing wrong?

我究竟做错了什么?

EDIT: The problem can be solved by adding the following lines:

编辑:问题可以通过添加以下几行来解决:

set(FAKE_TARGET fakeTarget)
add_custom_target(fakeTarget
    odb -o /home/david/dev/ --std c++11 -I/home/david/dev/ -d sqlite --generate-query --generate-schema ${PROMOTER_LIB_PREFIX}/entities/person.hpp
)
add_dependencies(${PROJECT_NAME} ${FAKE_TARGET})

回答by Pierre Fourgeaud

For me, with something similar, I just use :

对我来说,类似的东西,我只是使用:

add_custom_command(TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND odb -o /home/david/dev/ --std c++11 -I/home/david/dev/ -d sqlite --generate-    query --generate-schema ${PROMOTER_LIB_PREFIX}/entities/person.hpp
)

We don't use DEPENDSor VERBATIM.

我们不使用DEPENDSVERBATIM

The DEPENDSoption specify that the command must be executed only after that the project you gave to this option is built.

DEPENDS选项指定该命令必须在您提供给该选项的项目构建完成后才执行。

EDIT :

编辑 :

Note that the PRE_BUILD option is only supported on Visual Studio 7 or later. For all other generators PRE_BUILD will be treated as PRE_LINK.

请注意,PRE_BUILD 选项仅在 Visual Studio 7 或更高版本上受支持。对于所有其他生成器,PRE_BUILD 将被视为 PRE_LINK。

Maybe that's why it doesn't work for you.

也许这就是它对你不起作用的原因。

A work around could be (a bit ugly) :

解决方法可能是(有点难看):

  • Create a fake project
  • Add your custom command on it as POST_BUILD
  • Make you current project dependent on the fake one
  • 创建一个假项目
  • 将您的自定义命令添加为 POST_BUILD
  • 让你当前的项目依赖于假的

回答by graywolf

Way I'm using it is:

我使用它的方式是:

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gen_icinstrtab.hpp
    COMMAND xsltproc --output ${CMAKE_CURRENT_BINARY_DIR}/gen_icinstrtab.hpp ${CMAKE_SOURCE_DIR}/xml/genictabc.xslt ${CMAKE_SOURCE_DIR}/xml/icminstr.xml
)

add_executable(
    du4

    ${CMAKE_CURRENT_BINARY_DIR}/gen_icinstrtab.hpp
    .
    .
    .
)

The key was to add even .hpp files into add_executable block.

关键是将 .hpp 文件添加到 add_executable 块中。