SWIG 支持静态成员函数的继承
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?
SWIG doesn't wrap inherited static functions of derived classes. How it can be resolved?
Here is a simple illustration of the problem.
This is a simple C++ header file:
// 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 {};
The C++ source file just includes the header file:
// file test.cpp
#include "test.hpp"
The SWIG interface file just includes the C++ header file:
// file test.swig
%module test
%{
#include "test.hpp"
%}
%include "test.hpp"
Then I generate the swig wrapper code by this:
swig -c++ -tcl8 -namespace main.swig
And then I create a shared library by this:
g++ -fpic -Wall -pedantic -fno-strict-aliasing \
test.cpp test_wrap.cxx -o libtest.so
So when loading libtest.so in a tcl interpretor and trying to use the wrapped interface, it has the following behavior:
% 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 !!
So the question is how can i make SWIG to wrap D::stat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态函数只在父类B中定义正确吗?如:
不可调用正确吗?这就是为什么 SWIG 不包装函数...
至于如何访问该函数,SWIG 允许您从任何您想要的类中添加/隐藏/包装函数,因此可以“修复” SWIG 类提供对 stat() 的访问。
相信语法是这样的:
自从我接触 SWIG 以来已经有一段时间了,所以我可能记错了一些东西。
The static function is only defined in the parent
class B
correct? as in:Is not callable correct? Thats why SWIG doesn't wrap the function...
As to how you can get access to the function, SWIG allows you to add/hide/wrap functions from any class you want to, so it would be possible to "fix" the SWIG class to give access to
stat()
.Believe the syntax is something like:
Its been a while since I touched SWIG so I might be misremembering something.