CMake conditionally add Qt signals with preprocessor defines

I'm working on a Qt project using CMake as a build system, but I'm running into a bit of an issue.

For debugging purposes, I want to add some extra buttons to my application, which means I also need associated signals/slots for those buttons to connect them. Since they're purely for debugging, though, I only want them included in the application if it's built in debug mode. To achieve this, I'm using CMAKE_BUILD_TYPE. For example, I have the following code:

IF(NOT CMAKE_BUILD_TYPE)
    MESSAGE(STATUS "No CMAKE_BUILD_TYPE specified; defaulting to Debug mode.")
    SET(CMAKE_BUILD_TYPE "Debug")
ENDIF()

STRING(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
IF(CMAKE_BUILD_TYPE_LOWER MATCHES debug)
    SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -DMYAPP_DEBUG")
ENDIF()

This seems to be working well. If I enable "verbose makefile," I see -DMYAPP_DEBUG being passed to the compiler just like it should be. This doesn't seem to work though, with moc files.

I'm using a rather recent version of CMake (i.e., >= 2.8.9), so I have the new CMAKE_AUTOMOC feature turned on. However, when I have a header file that contain:

#ifdef MYAPP_DEBUG
    void debugTriggered(bool);
#endif

I get the following when I start the application:

QObject::connect: No such signal MainMenu::debugTriggered(bool)

I'm assuming the reason for this is that the value of CMAKE_CXX_FLAGS is not taken into account when CMake is AUTOMOC-ing my sources. How can I alter my CMake build file to get this working?

Answers


Macros should be added using add_definitions and not via CMAKE_CXX_FLAGS.

Another problem is that your flags are messed up - you use cmake_C_flags to load values into cmake_CXX_flags


Need Your Help

Going Back after connectionDidFinishLoading

iphone xcode json httprequest

I have a question, (Basically same as this one which remained UN-answered

LoopBack Automigrate Call Order

javascript mysql model loopbackjs strongloop

I am using LoopBack auto migrate to create MySQL database tables from models during app boot. My Account model define a hasAndBelongsToMany relationship with my Credit model, and visa versa.