sop
sop

Reputation: 3625

Why the macro is not defined by cmake?

I have the following in my cmake file:

#...
project(${EXECUTABLE_NAME})

set(CMAKE_CXX_FLAGS "-g -std=gnu++11")

option(DEBUG "Displays values and images at different steps" OFF)
if (DEBUG)
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -DDEBUGGING ")
    message("DEBUG = ON")
endif()
#...

And I have in my code something like this:

#ifdef DEBUGGING
    if (flgIn == DISPLAY)
        std::cout << val1 << " " << val2 << " " << val3 << std::endl;
#endif

When I do

$ cmake -DDEBUG=OFF ..
$ make

it is working fine. But when I set the option

$ cmake -DDEBUG=ON ..
$ make

I am getting the following error:

c++: fatal error: no input files
compilation terminated.
/bin/sh: 1: -DDEBUGGING: not found
make[2]: *** [CMakeFiles/prj.dir/src/cpp/file.cpp.o] Error 127

I do not understand what I have don wrong. Any suggestions, please?

Upvotes: 2

Views: 670

Answers (1)

steveire
steveire

Reputation: 11074

As in the comment, use target_compile_definitions

http://www.cmake.org/cmake/help/v3.0/command/target_compile_definitions.html

However, as for the problem you encountered, CMAKE_CXX_FLAGS is a string, not a list. You can add content to it by dealing with it as a string:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUGGING ")

Upvotes: 4

Related Questions