5

SWIG 不包装派生类的继承静态函数。如何解决?

这是问题的简单说明。

这是一个简单的 C++ 头文件:

// file test.hpp
#include <iostream>

class B
{
public:
  static void stat()
  { std::cerr << "=== calling static function B::stat" << std::endl; }

  void nonstat() const
  { std::cerr << "==== calling B::nonstat for some object of B" << std::endl; }
};

class D : public B {};

C++ 源文件只包含头文件:

// file test.cpp
#include "test.hpp"

SWIG 接口文件只包含 C++ 头文件:

// file test.swig
%module test
%{
#include "test.hpp"
%}

%include "test.hpp"

然后我通过以下方式生成 swig 包装器代码:

swig -c++ -tcl8 -namespace main.swig

然后我通过这个创建一个共享库:

g++ -fpic -Wall -pedantic -fno-strict-aliasing \
               test.cpp test_wrap.cxx -o libtest.so

因此,当在 tcl 解释器中加载 libtest.so 并尝试使用包装的接口时,它具有以下行为:

% load libtest.so test
% test::B b
% test::D d
% b nonstat    # works fine
% d nonstat    # works fine
% test::B_stat # works fine
% test::D_stat # DOESN'T WORK !!

所以问题是我怎样才能让 SWIG 包装 D:​​:stat?

4

1 回答 1

2

静态函数只在父级中定义对class B吗?如:

D::stat();

是不是可调用的正确?这就是 SWIG 不包装函数的原因......

至于如何访问该函数,SWIG 允许您从任何您想要的类中添加/隐藏/包装函数,因此可以“修复” SWIG 类以授予对stat().

相信语法是这样的:

%extend D {
   ...
}

自从我接触 SWIG 以来已经有一段时间了,所以我可能记错了一些东西。

于 2010-06-20T01:27:20.710 回答