Python导入&使用cdll(带有一个linux .so文件) [英] Python importing & using cdll (with a linux .so file)

查看:537
本文介绍了Python导入&使用cdll(带有一个linux .so文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最后一个问题关于python& c + +整合后,我被告知在Windows使用dll。
(上一个问题)

After one of my last questions about python&c++ integration i was told to use dlls at windows. (Previous question)

这样可以正常工作:

cl / LD A.cpp B.cpp C.pp

现在我试图做同样的linux,创建一个.so文件通过ctypes在python2.5上导入。
我做了:

Now i'm tryting to do the same in linux, creating a .so file to import through ctypes on python2.5. I did:

gcc -Wall -Wextra -pedantic A.cpp B.cpp C.cpp / usr / lib / libcryptopp .so -shared -o /test/decoding.so

,然后创建so对象。如果删除-shared编译是确定,但停止作为没有主要在那里(显然;))。当然libcryptopp.so也存在。

and the so object is created ok. If removed "-shared" compilation is OK but stops as no main in there (obviously ;) ). Of course libcryptopp.so exists too.

但是当我去python并导入so文件时,它说该属性没有对象decrypt加密或任何我放在那里。

But when i go to python and import the "so" file, it said that the attribute has no object "decrypt", "encrypt" or whatever i put there. using "dir" over the dll objects confirms that they are not there.

外部函数在A.cpp中定义为:

external functions are defined in A.cpp as:

int encrypt (params...)
//..
return num;

int decrypt (params...)
//..
return num;

也尝试使用:

extern "C" encrypt (params...)
.....


b $ b

有人能告诉我我在做什么吗?

Could anyone tell me what i'm doing wrong?

提前感谢!

Rag

推荐答案

C ++编译器会破坏函数名称。要做你想做的事你必须在里面有声明原型

C++ compiler mangles names of functions. To do what you are trying to do you must have the declaration prototype inside

extern "C" {...}

很难从你的样本中得知你在源文件中究竟有什么。
像已经提到的人一样,使用nm实用程序来查看你的共享对象中的对象。

it's hard to tell from your samples what exactly you have in a source file. As someone already mentioned, use nm utility to see what objects that are in your shared object.

不要编译你的对象没有-shared。 Python加载库不支持静态链接的对象,只要知道。

Do not compile your object without -shared. Python load library does not support statically linked objects as far as am aware.

使用g ++编译器编译你的对象,它将链接到标准C ++库,gcc不。

compile your object with g++ compiler instead, it will link to standard C++ Library, gcc does not.

这篇关于Python导入&使用cdll(带有一个linux .so文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆