Search code examples
macrosc-preprocessorqmake

Qmake DEFINES macro with parameter


I want to know how to define C++ preprocessor macro using qmake DEFINES variable. I have tried to do the following:

Project file

TEMPLATE = app
CONFIG += c++14 console

SOURCES += main.cpp

DEFINES  += "TEMPORARY_UNUSED(x)=\"(void)x;\""
DEFINES  += "BASE_CLASS_UNUSED(x)=\"(void)x;\""

Main.cpp file

int main() {
    int hello;
    TEMPORARY_UNUSED(hello)
    BASE_CLASS_UNUSED(hello)
}

But this resulted in following error: [main.o] Error 2. I have no idea how macro definition (very simple by the way) could cause errors in build process.

This is macro definitions using c++'s #define. They work just as I expected

#define TEMPORARY_UNUSED(x) (void)x;
#define BASE_CLASS_UNUSED(x) (void)x;

The question is: how do I define c++ preprocessor macro using qmake DEFINES and how my macro was able to cause compilation errors.

P.S. I'm perfectly aware of Q_UNUSED macro but I prefer to have a macro that indicates not only that the variable is unused but also why it is unused.

P.S. Code that I have posted is 100% of my project, there no more files that define/redefine anything else.


Solution

  • You need to escape the parentheses and the semicolon with backslashes:

    DEFINES += TEMPORARY_UNUSED\\(x\\)=\\(void\\)x\\;
    

    Otherwise the () and the ; in the -D compiler argument will be interpreted as parts of a more complex command.

    Note that the /D option of MSVC does not support function-like macro definitions. To overcome this restriction, write your preprocessor directives into a header file and include it with the /FI compiler switch.