Important: Please read the Qt Code of Conduct -

How to add entries to moc_predefs.h?

  • Moc generates file moc_predefs.h, that contains some defines, that are included to every file that moc parses. Is there a way to add defines to this file?

    I'd like to define some include guards in this file to prevent moc from parsing some headers. I could define these guards directly as moc's parameter, but that would unnecessarily bloat the makefile, it would be much nicer to have it directly in the predefs file.

    Thanks for any hints.

  • Lifetime Qt Champion


    Would it be simpler to use #ifdef Q_MOC_RUN in your code ?

  • @SGaist Hi, that is a possibility, but I'm trying to prevent moc from parsing 3rd party headers like boost... That would mean using #ifdef Q_MOC_RUN around every such include, which isn't exactly convenient.

    But I've just found out, that there's a command line parameter for moc @<file>, which enables to read additional command options from a file. It seems I can create a file full of -DINCLUDE_GUARD and pass it to moc in my pro file with QMAKE_MOC += @moc_ignored_file_guards.txt.

    I haven't yet tested it, but if it works, it would be as convenient as it gets for my case :)

  • Lifetime Qt Champion

    What is your problem with boost ?

  • @SGaist said in How to add entries to moc_predefs.h?:

    What is your problem with boost ?

    Moc is slow to parse it. I can speed up moc 2x-3x simply by not parsing boost headers. In some cases I tested, it seems I can speed up moc 10x if I prevent it to parse all 3rd party includes. This means a total save of several minutes of build time in my case.

    Btw. the solution I posted in my previous post seems to work, which basically solves my issue.

  • Lifetime Qt Champion

    Good to know, thanks for the feedback !

Log in to reply