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.
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.
Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With