Python ctypes和动态链接 [英] Python ctypes and dynamic linking

查看:107
本文介绍了Python ctypes和动态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C语言编写一些库,其中包含一些我想通过ctypes从Python调用的函数。

I'm writing some libraries in C which contain functions that I want to call from Python via ctypes.

我已经成功地完成了另一个库,但这库仅具有非常重要的依赖关系(即 fstream math malloc stdio stdlib )。我正在处理的另一个库具有更复杂的依赖性。

I've done this successfully another library, but that library had only very vanilla dependencies (namely fstream, math, malloc, stdio, stdlib). The other library I'm working on has more complicated dependencies.

例如,我将尝试使用 fftw3 。作为测试,我将尝试编译一个简单的 .cpp 文件,其中包含:

For example, I'll try to use fftw3. As a test, I'll just try to compile a simple .cpp file containing:

int foo()
{
    void *p  = fftw_malloc( sizeof(fftw_complex)*64 );
    fftw_free(p);

    printf("foo called.\n");

    return 0;
}        

我将其编译为:

icpc -Wall -fPIC -c waveprop.cpp -o libwaveprop.o $std_link
icpc -shared -Wl,-soname,libwaveprop.so.1 -o libwaveprop.so.1.0 libwaveprop.o 

cp waveprop.so.1.0 /usr/local/lib/
rm waveprop.so.1.0
ln -sf /usr/local/lib/waveprop.so.1.0 /usr/local/lib/waveprop.so
ln -sf /usr/local/lib/waveprop.so.1.0 /usr/local/lib/waveprop.so.1

这一切都可以。现在,我使用另一个包含以下内容的 .cpp 文件进行测试:

This all works. Now I test it with another .cpp file containing:

int main()
{
    foo();
}

结果:

icpc test.cpp -lwaveprop 
/lib/../lib64/libwaveprop.so: undefined reference to `fftw_free'
/lib/../lib64/libwaveprop.so: undefined reference to `fftw_malloc'

这是完全合理的。接下来我尝试:

Which is entirely reasonable. Next I try:

icpc test.cpp -lwaveprop -lfftw3
./a.out
foo called.

太好了!但是现在当我尝试使用ctypes加载库时:

Great! But now when I try to load the library with ctypes:

>>> from ctypes import *
>>> print cdll.LoadLibrary('/usr/local/lib/libwaveprop.so.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/ctypes/__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib64/python2.6/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /usr/local/lib/libwaveprop.so.1: undefined symbol: fftw_free

所以这是相同的问题,但是我不知道如何为ctypes解决它。我已经尝试了各种事情,但都没有成功,并且我还停留在这一点上。

So it's the same problem, but I have no idea how to resolve it for ctypes. I've tried various things without any success, and I'm pretty stuck at this point.

推荐答案

好的,谢谢您帮助。

要使其正常工作,我必须在链接(duh)时包括相关性。我之前曾尝试过此操作,但遇到错误,因此要解决此问题,必须将fftw用-fpic作为CPP标志重新编译。

to get this to work I had to include the dependencies when linking (duh). I had tried this before but got an error, so solve this I had to recompile fftw with '-fpic' as a CPP flag. all works now.

icpc -Wall -fPIC -c waveprop.cpp -o libwaveprop.o $std_link
icpc -shared -Wl,-soname,libwaveprop.so.1 -o libwaveprop.so.1.0 libwaveprop.o -lfftw3

cp waveprop.so.1.0 /usr/local/lib/
rm waveprop.so.1.0
ln -sf /usr/local/lib/waveprop.so.1.0 /usr/local/lib/waveprop.so
ln -sf /usr/local/lib/waveprop.so.1.0 /usr/local/lib/waveprop.so.1

感谢,
-尼克

thanks, -nick

这篇关于Python ctypes和动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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