使用 SWIG 的 C++ 级
我尝试从 C++ 代码创建一个 python 接口(使用 swig)。用下面的代码。当我从 .cpp 文件中删除行:时
aClass z = aClass(1);
,出现以下错误:
Traceback (most recent call last):
File "./testit.py", line 3, in <module>
import testlib
File "(...)/testlib.py", line 26, in <module>
_testlib = swig_import_helper()
File "(...)/testlib.py", line 22, in swig_import_helper
_mod = imp.load_module('_testlib', fp, pathname, description)
ImportError: (...)/_testlib.so: undefined symbol: _ZN6aClassC1Ei
我做错了什么?
testlib.cpp
#include <iostream>
#include <string.h>
using namespace std;
class aClass {
public:
aClass(int i) {
iD = i;
}
void printiD() {
cout << iD << endl;
}
private:
int iD;
};
void doSomething(string s) {
cout << "testlib: I did something with:" << s << endl;
}
void outprintiD(aClass ff) {
ff.printiD();
}
string returnSomething(string s) {
return s;
}
//Don't know why, but without the next line it doesn't work. :(
aClass z = aClass(1);
testlib.i
%module testlib
%include "std_string.i"
using namespace std;
%{
class aClass {
public:
aClass(int i);
void printiD();
private:
int iD;
};
void outprintiD(aClass ff);
void doSomething(std::string s);
std::string returnSomething(std::string s);
%}
class aClass {
public:
aClass(int i) ;
void printiD();
private:
int iD;
};
void outprintiD(aClass ff);
void doSomething(std::string s);
std::string returnSomething(std::string s);
testit.py
#!/usr/bin/python
import testlib
testlib.doSomething("doS");
var = testlib.returnSomething("rSo");
print var
aClassInstance = testlib.aClass(42)
testlib.outprintiD(aClassInstance)
print "done..."
执行脚本
swig -c++ -python $1.i
g++ -c -fPIC $1.cpp $1_wrap.cxx -I/usr/include/python2.7
g++ -shared $1.o $1_wrap.o -o _$1.so
I try to create a python interface (with swig) from C++-code. With the code below. When I remove the line:
aClass z = aClass(1);
from the .cpp-file i get the following error:
Traceback (most recent call last):
File "./testit.py", line 3, in <module>
import testlib
File "(...)/testlib.py", line 26, in <module>
_testlib = swig_import_helper()
File "(...)/testlib.py", line 22, in swig_import_helper
_mod = imp.load_module('_testlib', fp, pathname, description)
ImportError: (...)/_testlib.so: undefined symbol: _ZN6aClassC1Ei
What am I doing wrong?
testlib.cpp
#include <iostream>
#include <string.h>
using namespace std;
class aClass {
public:
aClass(int i) {
iD = i;
}
void printiD() {
cout << iD << endl;
}
private:
int iD;
};
void doSomething(string s) {
cout << "testlib: I did something with:" << s << endl;
}
void outprintiD(aClass ff) {
ff.printiD();
}
string returnSomething(string s) {
return s;
}
//Don't know why, but without the next line it doesn't work. :(
aClass z = aClass(1);
testlib.i
%module testlib
%include "std_string.i"
using namespace std;
%{
class aClass {
public:
aClass(int i);
void printiD();
private:
int iD;
};
void outprintiD(aClass ff);
void doSomething(std::string s);
std::string returnSomething(std::string s);
%}
class aClass {
public:
aClass(int i) ;
void printiD();
private:
int iD;
};
void outprintiD(aClass ff);
void doSomething(std::string s);
std::string returnSomething(std::string s);
testit.py
#!/usr/bin/python
import testlib
testlib.doSomething("doS");
var = testlib.returnSomething("rSo");
print var
aClassInstance = testlib.aClass(42)
testlib.outprintiD(aClassInstance)
print "done..."
execution script
swig -c++ -python $1.i
g++ -c -fPIC $1.cpp $1_wrap.cxx -I/usr/include/python2.7
g++ -shared $1.o $1_wrap.o -o _$1.so
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个链接器错误。尝试:
即交换目标文件。这是因为它们的顺序很重要,并且可以合理地假设
$1_wrap.o
想要从$1.o
中提取一些方法。It's a linker error. Try:
i.e. swap the object files. It's because the order they are given matters, and it's reasonable to suppose that
$1_wrap.o
wants to pull some methods from$1.o
.