如何使用Swig省略前向声明

huangapple go评论114阅读模式
英文:

how to swig omit forward declaration

问题

我正在尝试在供应商头文件上运行swig,这样我就不需要在单独的头文件中重新定义实现。

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

go build期间,我遇到了以下错误:

> $HOME/Qt5.9.1/5.9.1/gcc_64/include/QtWidgets/qapplication.h:57: 错误:
> 输入(1)中的语法错误。

检查了以下内容:

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

显然在前向声明上失败了。

以下是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>

我该如何更改以使swig省略前向声明?

英文:

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?

答案1

得分: 4

如果你真的需要告诉SWIG忽略源代码中的某些内容,你可以使用它定义的宏:文档

所以你的qapplication.h文件应该改成这样:

#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

不过要小心使用这种策略,因为你可能会导致SWIG预处理成功,但由于遗漏了一些重要内容而导致C++编译失败。

另一种方法是完全避免使用%include(从企业经验来说)。随着项目规模的增大,由于包含了头文件中的所有内容,SWIG的速度会越来越慢。最终,只复制SWIG需要的声明对于编译时间来说是值得的。

英文:

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 &lt;QtWidgets/qtwidgetsglobal.h&gt;
#include &lt;QtCore/qcoreapplication.h&gt;
#include &lt;QtGui/qwindowdefs.h&gt;
#include &lt;QtCore/qpoint.h&gt;
#include &lt;QtCore/qsize.h&gt;
#include &lt;QtGui/qcursor.h&gt;
#ifdef QT_INCLUDE_COMPAT
# include &lt;QtWidgets/qdesktopwidget.h&gt;
#endif
#include &lt;QtGui/qguiapplication.h&gt;

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.

huangapple
  • 本文由 发表于 2017年9月14日 19:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/46218476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定