C++ 计算'QT_CONFIG(打印机)时预处理器被零除`

C++ 计算'QT_CONFIG(打印机)时预处理器被零除`,c++,macros,qt5.9,C++,Macros,Qt5.9,使用以下命令行使用g++(来自使用qmake生成的Makefile)编译 #if !QT_CONFIG(printer) // do something #endif 给出两个g++(7.3.0)上的预处理器错误 和叮当声(6.00) 其中,clang++提供了更详细的输出打印机未启用,因此建议使用宏进行条件编译。QT版本是5.9.5。任何建议(错误用法?)都将不胜感激。我认为您不应该关注该宏。该宏的目的是在QT_FEATURE_printer为零时使编译代码崩溃。该代码的设计目的不是

使用以下命令行使用g++(来自使用qmake生成的Makefile)编译

#if !QT_CONFIG(printer)
    // do something
#endif
给出两个g++(7.3.0)上的预处理器错误

和叮当声(6.00)


其中,clang++提供了更详细的输出<代码>打印机未启用,因此建议使用宏进行条件编译。QT版本是5.9.5。任何建议(错误用法?)都将不胜感激。

我认为您不应该关注该宏。该宏的目的是在QT_FEATURE_printer为零时使编译代码崩溃。该代码的设计目的不是为了在其他情况下工作


不要有条件地使用宏,而是尝试找出QT_FEATURE_printer为零的原因,并包含/配置依赖项以更改该值(它似乎在printsupport/qtprintsupport config.h中定义)。

如果您使用新功能将QT源更新为某个内容,而不再次运行配置,则会发生这种情况。运行“配置”时,将设置新功能。

这在Qt 5.12.3中已修复。更新版本的notepad.cpp开始于:

#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
#if QT_CONFIG(printdialog)
#include <QPrintDialog>
#endif // QT_CONFIG(printdialog)
#include <QPrinter>   
#endif // QT_CONFIG(printer)
#endif // QT_PRINTSUPPORT_LIB
#include <QFont>
#include <QFontDialog>
#包括
#包括
#包括
#包括
#如果已定义(QT_打印支持_库)
#包括
#如果QT_配置(打印机)
#如果QT_配置(打印对话框)
#包括
#endif//QT_配置(打印对话框)
#包括
#endif//QT_配置(打印机)
#endif//QT\u打印支持\u库
#包括
#包括

也许可以直接检查打印机的
QT\u功能
?谢谢,就这样。仍然想知道,如果功能不可用,为什么重新推荐的实践会失败。如果未定义
QT\u功能\u打印机
,则
。\if(1/QT\u功能\u打印机==1)
将扩展到
。\if(1/0==1)
,这会由于被零除而导致错误。根据编译器和选项的不同,您还可能会收到关于打印机未定义的
QT\u功能的警告。谢谢。不过,QT的教程代码建议测试检查!如果没有功能,然后有条件地禁用按钮。也许QT应该更新他们的教程?
test.cpp:25:6: error: division by zero in preprocessor expression
#if !QT_CONFIG(printer)
     ^~~~~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:84:30: note: expanded from macro 'QT_CONFIG'
#define QT_CONFIG(feature) (1/QT_FEATURE_##feature == 1)
                            ~^~~~~~~~~~~~~~~~~~~~~
1 error generated.
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
#if QT_CONFIG(printdialog)
#include <QPrintDialog>
#endif // QT_CONFIG(printdialog)
#include <QPrinter>   
#endif // QT_CONFIG(printer)
#endif // QT_PRINTSUPPORT_LIB
#include <QFont>
#include <QFontDialog>