4

I am trying to run swig on vendor header so I do not need to redefine the implementation in a separate header file

swig -go -cgo -intgosize 64 -module qt -o $WORK/qt/_obj/qt_wrap.cxx -outdir $WORK/qt/_obj/ \ 
-I$HOME/Qt5.9.1/5.9.1/gcc_64/include -I$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtOpenGL \
-I$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtWidgets -I$HOME/Qt5.9.1/5.9.1/gcc_64 \ 
/include/QtGui -I$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtCore -c++ qt.swigcxx

I am getting the error below during go build

$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtWidgets/qapplication.h:57: Error: Syntax error in input(1).

Upon inspecting

qapplication.h:57

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qcoreapplication.h>
#include <QtGui/qwindowdefs.h>
#include <QtCore/qpoint.h>
#include <QtCore/qsize.h>
#include <QtGui/qcursor.h>
#ifdef QT_INCLUDE_COMPAT
# include <QtWidgets/qdesktopwidget.h>
#endif
#include <QtGui/qguiapplication.h>

QT_BEGIN_NAMESPACE


class QDesktopWidget;

Obviously fail on the forward declaration.

Below is qt.swigcxx:

// See swig.org for more inteface options,
// e.g. map std::string to Go string
%module qt
%{
#include <QPushButton>
#include <QApplication>
%}

%ignore "";


%include <qapplication.h>
%include <qpushbutton.h>

How can I change so that swig omits the forward declaration?

user642318
  • 387
  • 1
  • 6
  • 17

1 Answers1

4

If you really need to inform SWIG it should ignore something in the source code, you can use the macros it defines: Documentation

So your qapplication.h changes to this:

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qcoreapplication.h>
#include <QtGui/qwindowdefs.h>
#include <QtCore/qpoint.h>
#include <QtCore/qsize.h>
#include <QtGui/qcursor.h>
#ifdef QT_INCLUDE_COMPAT
# include <QtWidgets/qdesktopwidget.h>
#endif
#include <QtGui/qguiapplication.h>

QT_BEGIN_NAMESPACE

#ifndef SWIG
class QDesktopWidget;
#endif

Be careful with this tactic in general, though, you might end up doing something to make the SWIG preprocessor succeed, but then fail the C++ compilation because you left out something important.

Another approach is to avoid %include altogether (speaking from enterprise experience here). As your project grows large, SWIG will increasingly slow down as a result of including everything from your header files. In the end, just copying the declarations SWIG needs is worth it for the compilation times.

AndyG
  • 39,700
  • 8
  • 109
  • 143
  • thanks. I prefer the second option and now stuck with typemap – user642318 Sep 14 '17 at 13:06
  • The reason why you get the error is either due to SWIG cannot recurse header files. If you are extremely strict with interfaces (avoid header-only stuff) and include headers in the order that they are needed, SWIG can be used for wrapping quite large projects. I work on a project with nearly 40.000 lines of generated code. The SWIG parts builds way faster than the QT stuff – Jens Munk Sep 14 '17 at 19:53