8

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

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

How should I do it?

I've seen set_target_properties but this only works for libraries and not for executables.

For example I want that the following program,

 int main()
 {

    cout << BUILDTYPE << endl;
 }

it's compiled in 3 different flavors (3 executables) based on the 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. Some hint? and I get 3 executables which prints

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
linello
  • 8,451
  • 18
  • 63
  • 109

2 Answers2

9

Are you sure that set_target_properties does not work? How about this one:

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

or:

set_target_properties(myAppV1 PROPERTIES COMPILE_DEFINITIONS "BUILDTYPE=1")

On my machine it works:

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")
Anonymous
  • 18,162
  • 2
  • 41
  • 64
  • Thank you! I have to move the set_target_properties after the add_executable and use it in the second version you wrote add_executable(myAppv1 main.cpp) set_target_properties(myAppV1 PROPERTIES COMPILE_DEFINITIONS "BUILDTYPE=1") but NOT this way add_executable(myAppv1 main.cpp) set_target_properties(myAppV1 PROPERTIES COMPILE_DEFINITIONS "-DBUILDTYPE=1") – linello Apr 03 '12 at 09:23
0

Another way could be:

mkdir two directory
buildflavor1
buildflavor2

In the first sub directory run:

cmake -DFLAVOR=OPTION1 ..

in the second run:

run cmake -DFLAVOR=OPTION2 ..

So two executable with same name with different compilation flag with is own feature .o and so on.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
bdbSOG
  • 1