2

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.

Jaa-c
  • 5,017
  • 4
  • 34
  • 64
  • AFAIK, moc will not parse headers not listed in the project file. Since these are 3rd party headers, can't you just omit them from the project file's `HEADERS` variable? – Nikos C. Apr 09 '19 at 16:20
  • @NikosC.: I think it does. I don't have these includes in the `HEADERS` variable, but if I wrap the includes with `#ifndef Q_MOC_RUN`, moc is 2x faster. I can't see any other reason why it would be so much slower other than it parses the includes. – Jaa-c Apr 09 '19 at 16:26

3 Answers3

3

I didn't check it but I hope this might work:

myproject.pro

...
# add this to the end of file
QMAKE_CXXFLAGS += -Dfoo
load(moc)
QMAKE_CXXFLAGS -= -Dfoo
CONFIG -= moc

I'm looking for a way to tell qmake / moc to add some extra defines to the files.

The problem is that moc_predefs.h is generated by C preprocessor (e.g. g++ -E -dM $$QMAKE_CXXFLAGS ...), not by moc itself (in the latter case QMAKE_MOC_OPTIONS would do the trick). So QMAKE_CXXFLAGS is the only relevant variable here.

Matt
  • 13,674
  • 1
  • 18
  • 27
0

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
Jaa-c
  • 5,017
  • 4
  • 34
  • 64
-1

If you have access to Cmake, you could configure your build to use AUTOMOC, which will selectively run when it encounters a moc file dependency, and also provides the SKIP_AUTOMOC controll to exclude files from being MOC'd

Tzalumen
  • 652
  • 3
  • 16
  • 1
    That doesn't answer the question. If I wanted to add the defines to the auto-generated file manually, I would just add them... – Jaa-c Apr 09 '19 at 16:04
  • I have revised my answer with a suggestion. – Tzalumen Apr 09 '19 at 16:10
  • That looks cool, unfortunately in my case, I have a qmake project. – Jaa-c Apr 09 '19 at 16:18
  • Cmake is a configurator/buildsystem generator, It is capable of generating your Qt Creator projects for you. – Tzalumen Apr 09 '19 at 16:26
  • That's true, but the project I work on is quite large, I can't just switch it to cmake in a reasonable time frame. – Jaa-c Apr 09 '19 at 16:30
  • Yeah, that's true. I was going to suggest you try the solution in the other answer... but the other answer is gone? – Tzalumen Apr 09 '19 at 16:32