Pietro
Pietro

Reputation: 13162

How to define a CMake macro in terms of another one?

How to define a CMake macro in terms of another one?

add_definitions(-DMACRO1=1)
add_definitions(-DMACRO2=2)

add_definitions(-DMACRO3=${MACRO1})   # no effect

message( "COMPILE_DEFINITIONS = ${DirDefs}" )

This is the output I would like:

COMPILE_DEFINITIONS = MACRO1=1;MACRO2=2;MACRO3=1

This is what I get:

COMPILE_DEFINITIONS = MACRO1=1;MACRO2=2;MACRO3=

Upvotes: 1

Views: 524

Answers (2)

Johannes S.
Johannes S.

Reputation: 4626

Calling add_definitions(-DMACRO1=1) simply adds this definition to your compiler's command-line. Its value is equivalent to a #define directive in your source code. It does not create a CMake variable.

So, in your case,${MACRO1} evaluates to the empty string, resulting in

ADD_DEFINITIONS(-DMACRO3=)

To make it work, use SET(...) to define the variable in CMake:

set(MACRO1 1)
add_definitions(-DMACRO1=${MACRO1})
add_definitions(-DMACRO2=2)

add_definitions(-DMACRO3=${MACRO1})

message( "COMPILE_DEFINITIONS = ${DirDefs}" )

Upvotes: 2

fenix688
fenix688

Reputation: 2615

The only thing that comes to my mind is executing at command line:

$ cmake -DMACRO1=1 -DMACRO2=2 .

With this command, we make sure that these variables'll exist when you execute your CMakeLists.txt. So you can use them in this one.

Then, create in your CMakeLists.txt an auxiliar variable to make sure the value is right:

set(_MACRO3 ${MACRO1})
MESSAGE("MACRO3 value = ${_MACRO3}")
ADD_DEFINITIONS(-DMACRO3=${_MACRO3})

I think if you only write:

ADD_DEFINITIONS(-DMACRO3=${MACRO1})

It'd be right too.

Upvotes: 1

Related Questions