我想用C++在Linux上生成一个SO文件,使用Python调用库。在Sub C中使用SyrdypTR,编译成功,但是调用库时使用Python会出错。
我创建了一些cpp文件,如下所示:
计算器。h:#ifndef HELLO_WORLD_CALCULATOR_H
#define HELLO_WORLD_CALCULATOR_H
class Calculator {
public:
double add(double first, double second);
};
#endif
在计算器.cpp公司名称:
^{pr2}$
在主.cpp公司名称:#include
#include
#include "Calculator.h"
using namespace std;
double add1(double first, double second){
shared_ptrcalculater(new Calculator());
return calculater->add(first, second);
}
extern "C"
{
double add(double f, double s){
return add1(f,s);
}
}
我用这个命令编译文件:
g++主.cpp-fPIC-共享-o测试.so-标准=c++11
我的python文件测试.py公司名称:# coding=utf-8
import ctypes
from ctypes import *
if __name__ == "__main__":
lib = ctypes.CDLL("./test.so")
lib.add.argstype = [c_double, c_double]
lib.add.restype = c_double
print lib.add(c_double(12), c_double(36))
我用python运行程序测试.py在终端。那么我得到了一个错误:Traceback (most recent call last):
File "test.py", line 9, in
lib = ctypes.CDLL("./test.so")
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: ./test.so: undefined symbol: _ZN10Calculator6devideEdd
如何修复错误?在