Python 使用 SWIG 的 C++ 包装器
Python wrapper for C++ using SWIG
我将使用 SWIG 为一些 C++ 代码编写一个 python 包装器。主要的 class 是 Cryptographer
,它使用两个 static 库,它们是 libgmp.a and libgmpxx.a 。所以我的代码是这样的(为简单起见,删除了一些实现代码):
example.h:
/* example.h */
#include <string.h>
#include <string>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include "gmp.h"
#include "gmpxx.h"
using namespace std;
class Cryptographer {
private:
mpz_class num;
public:
Cryptographer();
virtual ~Cryptographer();
};
example.cpp:
/* example.cpp */
#include "example.h"
Cryptographer::Cryptographer() {
num = 12;
}
Cryptographer::~Cryptographer() {
}
针对以上两个文件,创建了一个SWIG接口:
example.i:
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "example.h"
然后我 运行 这些命令:(取自 here)
swig -c++ -python example.i // creates "example_wrap.cxx"
gcc -c -fPIC example_wrap.cxx -I/usr/include/python2.7 // outputs "example_wrap.o"
gcc -c -fPIC example.cpp -I/usr/include/python2.7 // creates "example.o"
g++ -shared example_wrap.o example.o -o example.so // creates "example.so"
然后我尝试在 python2.7 中导入 example
模块,但不幸的是它不起作用:
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./example.so: undefined symbol: __gmpz_set_si
我猜那些 libgmp 和 libgmpxx 库在链接过程中没有正确链接但我没有知道如何修复它。
顺便说一句,所有需要的文件都可以通过 this link.
访问
这在 gcc 6.3.0 中工作正常:
g++ -shared -o _example.so example_wrap.o example.o libgmp.a libgmpxx.a
不过,您可能需要提供 .a
文件的完整路径。
并且这些库需要位置独立代码(又名 -fPIC)编译。
要重建库:
1) 下载这个:https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2
2) 将文件解压缩到所选目录中。
3) 从那个目录,运行 这个命令:
./configure --with-pic=yes --enable-cxx
如果一切顺利,您将获得 Makefile
。
所以在之后立即调用 make
和 make install
。完毕。
我将使用 SWIG 为一些 C++ 代码编写一个 python 包装器。主要的 class 是 Cryptographer
,它使用两个 static 库,它们是 libgmp.a and libgmpxx.a 。所以我的代码是这样的(为简单起见,删除了一些实现代码):
example.h:
/* example.h */
#include <string.h>
#include <string>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include "gmp.h"
#include "gmpxx.h"
using namespace std;
class Cryptographer {
private:
mpz_class num;
public:
Cryptographer();
virtual ~Cryptographer();
};
example.cpp:
/* example.cpp */
#include "example.h"
Cryptographer::Cryptographer() {
num = 12;
}
Cryptographer::~Cryptographer() {
}
针对以上两个文件,创建了一个SWIG接口:
example.i:
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "example.h"
然后我 运行 这些命令:(取自 here)
swig -c++ -python example.i // creates "example_wrap.cxx"
gcc -c -fPIC example_wrap.cxx -I/usr/include/python2.7 // outputs "example_wrap.o"
gcc -c -fPIC example.cpp -I/usr/include/python2.7 // creates "example.o"
g++ -shared example_wrap.o example.o -o example.so // creates "example.so"
然后我尝试在 python2.7 中导入 example
模块,但不幸的是它不起作用:
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./example.so: undefined symbol: __gmpz_set_si
我猜那些 libgmp 和 libgmpxx 库在链接过程中没有正确链接但我没有知道如何修复它。
顺便说一句,所有需要的文件都可以通过 this link.
访问这在 gcc 6.3.0 中工作正常:
g++ -shared -o _example.so example_wrap.o example.o libgmp.a libgmpxx.a
不过,您可能需要提供 .a
文件的完整路径。
并且这些库需要位置独立代码(又名 -fPIC)编译。
要重建库: 1) 下载这个:https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2 2) 将文件解压缩到所选目录中。 3) 从那个目录,运行 这个命令:
./configure --with-pic=yes --enable-cxx
如果一切顺利,您将获得 Makefile
。
所以在之后立即调用 make
和 make install
。完毕。