如何使用SWIG公开一个公共的内联c++方法

How do I use SWIG to expose a public inlined c++ method?

本文关键字:c++ 一个 方法 SWIG 何使用      更新时间:2023-10-16

假设我有以下类:

class bar
{
public:
    bar();
    void helloworld(int date) 
         { std::cout << "Hello world, the date is: " << date << std::endl; }
};

我必须在接口文件中做什么才能公开内联的helloworld()方法?我查看了%内联,但它似乎并没有达到我想要的效果,即将其公开为一个可调用的方法。

如有任何帮助,将不胜感激

你可以这样做:

/* bar_module.i */
%module bar_module
%{
#include "bar.h"
%}
%include "bar.h"
/* bar.h */
#include <iostream>
class bar
{
public:
    bar();
    void helloworld(int date) 
         { std::cout << "Hello world, the date is: " << date << std::endl; }
};
/* bar.cc */
#include "bar.h"
bar::bar() {}
# test.py
import bar_module
bar_module.bar().helloworld(47)
# Build commands
swig -o bar_module_wrap.cc -python -c++ bar_module.i
g++ -o bar_module_wrap.os -c -fPIC -I/usr/include/python2.7 bar_module_wrap.cc
g++ -o bar.os -c -fPIC -I/usr/include/python2.7 bar.cc
g++ -o _bar_module.so -shared bar_module_wrap.os bar.os
# Test command
python test.py
# Output
Hello world, the date is: 47