Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

build

cmake

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.

like image 422
Mark Avatar asked Mar 21 '11 23:03

Mark


People also ask

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What does target link libraries do in CMake?

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.

How do you trigger CMake?

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.


1 Answers

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)
like image 188
Jack Kelly Avatar answered Sep 26 '22 01:09

Jack Kelly