How to create a program with two different variable values ​​in CMake

I recently ported my Qt project from qmake to CMake . My main program contains a value that depends on the #define directive.

I want to indicate that the define directive is externally via CMake and build 3 named versions of the same executable.

How can I do it?

I saw set_target_properties , but this only works for libraries, not for executables.

For example, I want the following program,

  int main() { cout << BUILDTYPE << endl; } 

it is compiled in 3 different versions (3 executable files) based on BUILDTYPE "define" For example, in my CMakeLists.txt I want to specify

 add_executable(myAppV1 -DBUILDTYPE=1) add_executable(myAppV2 -DBUILDTYPE=2) add_executable(myAppV3 -DBUILDTYPE=3) 

but this is not the correct syntax. Any hint? and I get 3 executables that print

+8
c ++ build cmake compiler-flags
source share
2 answers

Are you sure set_target_properties not working? How about this:

 set_target_properties(myAppV1 PROPERTIES COMPILE_FLAGS "-DBUILDTYPE=1") 

or

 set_target_properties(myAppV1 PROPERTIES COMPILE_DEFINITIONS "BUILDTYPE=1") 

It works on my machine:

 add_executable(myAppV1 main.cpp) add_executable(myAppV2 main.cpp) set_target_properties(myAppV1 PROPERTIES COMPILE_DEFINITIONS "BUILDTYPE=1") set_target_properties(myAppV2 PROPERTIES COMPILE_DEFINITIONS "BUILDTYPE=2") 
+9
source share

Another way:

 mkdir two directory buildflavor1 buildflavor2 

In the first run of the subdirectory:

 cmake -DFLAVOR=OPTION1 .. 

in the second run:

 run cmake -DFLAVOR=OPTION2 .. 

So, two executables with the same name with a different compilation flag with their own function .o , etc.

0
source share

All Articles