4

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.

pba
  • 524
  • 3
  • 13
  • Maybe check for `QT_FEATURE_printer` directly instead? – Some programmer dude Apr 24 '19 at 07:01
  • thanks, that does it. Still wondering why the reommended practice fails, if the feature is not available. – pba Apr 24 '19 at 07:11
  • If `QT_FEATURE_printer` is not defined then `#if (1/QT_FEATURE_printer == 1)` will expand to `#if (1/0 == 1)`, which causes an error due to division by zero . Depending on compiler and options, you may also get a warning about `QT_FEATURE_printer` being undefined. – Peter Apr 24 '19 at 07:48

3 Answers3

3

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

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).

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • Thanks. However, tutorial code of QT proposes the test to check! for the absence of a feature and then e.g. conditionally disable buttons. Maybe QT should then update their tutorials? – pba Apr 24 '19 at 17:36
2

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.

Allan Jensen
  • 518
  • 4
  • 8
  • 2
    I don't think that is right : I am getting this error for instance wiht QT_CONFIG(vulkan) with the official ubuntu packages on a CI pipeline: https://github.com/ossia/score/runs/3144941727?check_suite_focus=true – Jean-Michaël Celerier Jul 23 '21 at 20:55