我将使用SWIG为一些c++代码编写一个python包装器。主类是Cryptographer
,它使用两个静态库,它们是libgmp.a和libgmpxx.a。所以我的代码是这样的(为了简单起见,删除了一些实现代码):
示例。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();
};
示例.cpp:
^{pr2}$对于上面的两个文件,创建了一个SWIG接口:
示例。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库在链接过程中没有正确链接,但我不知道如何修复它。在
{/BTW可访问所有文件
这在gcc 6.3.0中运行良好:
但是,您可能需要提供
.a
文件的完整路径。在这些库需要一个位置无关的代码(aka-fPIC)编译。在
重建库: 1) 下载这个:https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2 2) 在选择的目录中提取文件。 3) 从该目录运行以下命令:
^{pr2}$如果一切顺利,您将得到一个
Makefile
。 因此,请在之后立即调用make
,和make install
。完成。在相关问题 更多 >
编程相关推荐