Search code examples
c++qtc-preprocessormoc

Force the Qt MOC to expand some custom macros?


Is it possible to force the Qt MOC to expand some of my custom preprocessor defines to achieve some kind of 2-pass preprocessing? E.g.:

// MOC macro
@MOC #define add(a, b) (a + b) // Should be expanded by MOC (1st run)

// "Normal" macro
#define sub(a, b) (a - b) // Should be expanded by preprocessor (2nd run)

Solution

  • If your goal is to enable or disable some macros only during the Moc pass, you can test the Q_MOC_RUN define as hinted by peppe.

    #ifdef Q_MOC_RUN
    #define MyMacro valueDuringMoc
    #else
    #define MyMacro defaultValue
    #endif
    

    If you want to build your own pre-processor rules relying on the same mechanism as MOC, i.e. generating additional C++ code in a .h files included in final build, then you should have a look at that other question.