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 接口(interface)文件只包含 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 并尝试使用包装接口(interface)时,它具有以下行为:
% 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?
最佳答案
静态函数只定义在父类 class B
对吗?如:
D::stat();
不可调用是否正确?这就是 SWIG 不包装函数的原因......
至于如何访问该函数,SWIG 允许您添加/隐藏/包装您想要的任何类的函数,因此可以“修复”SWIG 类以访问 统计()
。
相信语法是这样的:
%extend D {
...
}
我已经有一段时间没有接触 SWIG 了,所以我可能记错了什么。
关于c++ - SWIG 支持继承静态成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3077236/