c++qtqmakemoc

How to add defines to moc_predefs.h?


Qt's moc.exe generates files moc_predefs.h, that contain some defines, that are included to every file that moc parses. Is there a way to add defines to these files?

I'd like to define some include guards in these files to prevent moc.exe from parsing some headers (mostly 3rd party headers like boost). I could define these guards directly as moc's parameter using QMAKE_MOC += -Dfoo, but that would unnecessarily bloat the makefiles, it would be much nicer to have it directly in the moc_predefs.hfile.

I've noticed that flags defined in the pro file e.g. QMAKE_CXXFLAGS += -Dfoo produce defines in moc_predefs.h. But obviously I don't want to define include guards for the build, just for moc...

Note I don't want to modify the generated files directly, I'm looking for a way to tell qmake / moc to add some extra defines to the files.


Solution

  • It proved to be quite difficult to add defines to moc_predefs.h, but there is another way to achieve the same result. Moc has a command line option @<file>, that enables to read additional moc command line options from a file. So it is possible to create a text file containing defines like -Dfoo, one per line.

    This file can be added to moc's command line parameters in a *.pro file:

    load(moc)
    QMAKE_MOC += @moc_ignored_file_guards.txt