简体   繁体   English

我应该尽可能多地使用 #ifndef Q_MOC_RUN 吗?

[英]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:我有一个使用 Boost 库的 Qt/C++ 项目,我看到 Boost 头文件是这样包含的:

#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.我读到如果你不这样做,MOC 可能会导致问题。

The question is, shouldn't I then use this guard for including all other headers that definitely don't contain Q_OBJECT marco?问题是,我不应该使用这个保护来包含所有其他绝对不包含 Q_OBJECT marco 的头文件吗? Standard library headers for example, and other non-Qt libraries?例如标准库头文件和其他非 Qt 库? Wouldn't it save a lot of time when MOC preprocessor runs? MOC预处理器运行的时候不是可以节省很多时间吗?

From the topic Qt5 moc error in combination with boost :从主题Qt5 moc error 结合 boost

First, this is a known MOC issue.首先,这是一个已知的 MOC 问题。 The MOC can't expand some of the macros used in the Boost library. MOC 无法扩展 Boost 库中使用的某些宏。 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.我相信 Qt 4.8 工作的原因是特定 Boost 宏定义的解决方法已添加到该版本的 MOC 配置中。

What you need to do to work around this issue: As stated above, use Q_MOC_RUN to comment out problematic headers.您需要做些什么来解决这个问题:如上所述,使用 Q_MOC_RUN 注释掉有问题的标题。 You ONLY need to use Q_MOC_RUN in files that produce a moc file (eg myheader.h will produce moc_myheader.cpp).您只需要在生成 moc文件的文件中使用 Q_MOC_RUN (例如 myheader.h 将生成 moc_myheader.cpp)。 You can limit the hack to just those files.您可以将 hack 限制为仅这些文件。 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.所以你不需要在你的项目中 #ifndef Boost 头文件的所有用法,这大大限制了实现这个解决方案的痛苦。

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.看来这个问题很久以前就已经解决了,所以不管你没有任何问题,不需要支持旧版本的Qt,你可能不会在以后的代码中添加这个宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM