【问题标题】:SWIG issue, empty files being generatedSWIG 问题,正在生成空文件
【发布时间】:2011-10-05 17:48:55
【问题描述】:

我正在尝试使用 SWIG 包装 C++ 类以创建 Java 接口,但是当我在我的文件上运行 SWIG 时,它会生成空文件。

我正在关注此处的示例:http://www.opensource.apple.com/source/swig/swig-4/swig/Examples/java/class/index.html?txt

我有一个这样的头文件:

#include <list>
#include <set>
#include <map>
#include <Heap.h>
#include "Controller.h"
namespace Arbitrator
{
  template <class _Tp,class _Val>
  class Arbitrator
  {
  public:
    Arbitrator();
    bool setBid(Controller<_Tp,_Val>* c, _Tp obj, _Val bid);
    bool setBid(Controller<_Tp,_Val>* c, std::set<_Tp> objs, _Val bid);
    bool removeBid(Controller<_Tp,_Val>* c, _Tp obj);
    bool removeBid(Controller<_Tp,_Val>* c, std::set<_Tp> objs);
    bool removeAllBids(Controller<_Tp,_Val>* c);
    bool accept(Controller<_Tp,_Val>* c, _Tp obj, _Val bid);
    bool accept(Controller<_Tp,_Val>* c, std::set<_Tp> objs, _Val bid);
    bool accept(Controller<_Tp,_Val>* c, _Tp obj);
    bool accept(Controller<_Tp,_Val>* c, std::set<_Tp> objs);
    bool decline(Controller<_Tp,_Val>* c, _Tp obj, _Val bid);
    bool decline(Controller<_Tp,_Val>* c, std::set<_Tp> objs, _Val bid);
    bool hasBid(_Tp obj) const;
    const std::pair<Controller<_Tp,_Val>*, _Val>& getHighestBidder(_Tp obj) const;
    const std::list< std::pair<Controller<_Tp,_Val>*, _Val> > getAllBidders(_Tp obj) const;
    const std::set<_Tp>& getObjects(Controller<_Tp,_Val>* c) const;
    void onRemoveObject(_Tp obj);
    _Val getBid(Controller<_Tp,_Val>* c, _Tp obj) const;
    void update();
  private:
    std::map<_Tp,Heap<Controller<_Tp,_Val>*, _Val> > bids;
    std::map<_Tp,Controller<_Tp,_Val>* > owner;
    std::map<Controller<_Tp,_Val>*, std::set<_Tp> > objects;
    std::set<_Tp> updatedObjects;
    std::set<_Tp> objectsCanIncreaseBid;
    std::set<_Tp> unansweredObjected;
    bool inUpdate;
    bool inOnOffer;
    bool inOnRevoke;
  };

  template <class _Tp,class _Val>
  Arbitrator<_Tp,_Val>::Arbitrator()
  {
    inUpdate=false;
    inOnOffer=false;
    inOnRevoke=false;
  }

  //other code removed to save space

我创建的接口文件如下所示:

/* arb.i */
%module arb
%{
#include "Arbitrator.h"
%}

/* grab header file */
%include "Arbitrator.h"

但是当我运行 SWIG 时:

swig -c++ -java arb.i

SWIG 创建的 Java 文件是空的。

有没有人遇到过这个问题/知道如何解决这个问题?

谢谢

【问题讨论】:

    标签: java java-native-interface swig


    【解决方案1】:

    您必须使用 %template 指令明确指示 SWIG 为模板生成代码。

    来自SWIG 2.0 documentation on templates

    SWIG 支持处理模板,但默认情况下,它 不会生成任何成员变量或函数包装器 模板类。为了创建这些包装器,您需要 明确告诉 SWIG 实例化它们。这是通过 %template 指令。

    来自SWIG 2.0 Templates chapter的一些简单例子:

    /* Instantiate a few different versions of the template */
    %template(intList) List<int>;
    %template(doubleList) List<double>;
    

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2018-03-20
      相关资源
      最近更新 更多