开发者

Get CMake to execute a target in project before building a library

I am using CMake to build my C+开发者_如何学编程+ project and it has multiple executables and a library (all part of same project). All is working fine, however one of my executables is a code generator that creates some of the library classes. I have got all the generation working but can't figure out how to call this executable (codegen) just before the library is built. I am on Linux environment. Hope someone can answer this.


In CMakeLists.txt:

First, define your executable:

add_executable(marks-code-generator gen.cpp)

Then, define a custom command to generate the source:

add_custom_command(OUTPUT generated.cpp generated.hpp
                   COMMAND marks-code-generator ARGS args here maybe
                   MAIN_DEPENDENCY input-file.in
                   DEPENDS marks-code-generator
                   COMMENT here we go!
                   VERBATIM)

The option VERBATIM makes sure platform-specific escaping is done correctly. The COMMENT will be printed out as make executes, giving something like [ 66%] here we go!.

Finally, name your generated source in the source list for your real program:

add_executable(some-program generated.cpp generated.hpp non-generated.cpp foo.cpp)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜