Preprocessor division by zero while evaluating `QT_CONFIG(printer)`

3k views Asked by At

Compiling with g++ (from Makefile generated with qmake) using the line

#if !QT_CONFIG(printer)
    // do something
#endif

gives a preprocessor error on both g++ (7.3.0)

test.cpp:25:6: error: division by zero in #if
 #if !QT_CONFIG(printer)

and clang (6.00)

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.

where clang++ gives the more detailed output. printer is not enabled, thus the macro as recommended to do conditional compilation. The QT version is 5.9.5. Any suggestions (wrong usage?) appreciated.

3

There are 3 answers

1
cprogrammer On BEST ANSWER

I don't think you should focus on that macro. The point of that macro is to simply crash your compilation code when QT_FEATURE_printer is zero. The code was not designed to work otherwise.

Instead of using the macro conditionally try to find out why QT_FEATURE_printer is zero and include / configure dependencies to change that (it seems to be definend in printsupport/qtprintsupport-config.h).

1
Allan Jensen On

This is what happens if you have updated your qt sources to something using a new feature without running configure again. When you run configure the new features are set.

0
user11545776 On

This is fixed in Qt 5.12.3. The newer version of notepad.cpp begins:

#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>