c++qtboostincludemoc

Should I use #ifndef Q_MOC_RUN as much a possible?


I have a Qt/C++ project that uses Boost library, and I see Boost headers are included like this:

#ifndef Q_MOC_RUN
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#endif

I read that if you don't do this, MOC might cause problems.

The question is, shouldn't I then use this guard for including all other headers that definitely don't contain Q_OBJECT marco? Standard library headers for example, and other non-Qt libraries? Wouldn't it save a lot of time when MOC preprocessor runs?


Solution

  • From the topic Qt5 moc error in combination with boost:

    First, this is a known MOC issue. The MOC can't expand some of the macros used in the Boost library. I believe the reason Qt 4.8 works is that a workaround for specific Boost macro definitions was added to the MOC configuration for that version.

    What you need to do to work around this issue: As stated above, use Q_MOC_RUN to comment out problematic headers. You ONLY need to use Q_MOC_RUN in files that produce a moc file (e.g. myheader.h will produce moc_myheader.cpp). You can limit the hack to just those files. So you don't need to #ifndef all usages of Boost headers in your project which limits the pain of implementing this solution quite a bit.

    Seems this issue was fixed quite a long time ago, so whether you don't have any problems and don't need to support old versions of Qt, you may won't add this macro to your future code.