简体   繁体   English

python ctypes用示例gsl gslcblas链接多个共享库

[英]python ctypes link multiple shared library with example gsl gslcblas

I want to use some functions from a shared library in python. 我想使用python共享库中的某些功能。 From the python doc, I know ctypes is a good choice. 从python文档中,我知道ctypes是一个不错的选择。 However such library have some undefined symbols and I should link it to another shared library to get the symbols. 但是,这样的库有一些未定义的符号,我应该将其链接到另一个共享库以获取这些符号。

In g++, it is simple: g++ main.cpp -la -lb. 在g ++中,它很简单:g ++ main.cpp -la -lb。 The function I need is in liba.so, and liba.so has some undefined function which can be solved in libb.so. 我需要的功能在liba.so中,而liba.so具有一些未定义的功能,可以在libb.so中解决。

But how to do that in ctypes? 但是如何在ctypes中做到这一点? ctypes.cdll.LoadLibrary('liba.so') said that there are some undefined symbols, how to tell ctypes to find libb.so? ctypes.cdll.LoadLibrary('liba.so')表示存在一些未定义的符号,如何告诉ctypes查找li​​bb.so? Because ldd liba.so not show a link to libb.so. 因为ldd liba.so没有显示指向libb.so的链接。

Example: I want to use some functions in gsl. 示例:我想在gsl中使用某些功能。 In g++: 在g ++中:

g++ main.cpp -lgsl -lgslcblas

and ldd libgsl.so does not show a link to libgslcblas 和ldd libgsl.so不显示指向libgslcblas的链接

In python: 在python中:

ctypes.cdll.LoadLibrary('libgsl.so')

how to tell ctypes to find libgslcblas? 如何告诉ctypes查找li​​bgslcblas?

The same problem also happen if I use scalapack. 如果我使用scalapack,也会发生相同的问题。 I use ubuntu 16.04 我使用Ubuntu 16.04

This old answer tells to apply mode=ctypes.RTLD_GLOBAL , ie in this case 这个旧答案告诉应用mode=ctypes.RTLD_GLOBAL ,即在这种情况下

import ctypes

dll1 = ctypes.CDLL('libgslcblas.so', mode=ctypes.RTLD_GLOBAL)
dll2 = ctypes.CDLL('libgsl.so')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM