使用 SWIG 的 C++ 级

发布于 2024-12-10 02:15:11 字数 2122 浏览 0 评论 0原文

我尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-12-17 02:15:11
vines@Aspire-5755G:~$ c++filt _ZN6aClassC1Ei
aClass::aClass(int)

这是一个链接器错误。尝试:

g++ -shared $1_wrap.o $1.o -o _$1.so

即交换目标文件。这是因为它们的顺序很重要,并且可以合理地假设 $1_wrap.o 想要从 $1.o 中提取一些方法。

vines@Aspire-5755G:~$ c++filt _ZN6aClassC1Ei
aClass::aClass(int)

It's a linker error. Try:

g++ -shared $1_wrap.o $1.o -o _$1.so

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文