[英]How to load .so libraryes in my Python app that is running on Ubuntu
I have some shared object files (.so libs) that I need to load in my python project that will run on Ubuntu platform. 我有一些共享对象文件(.so libs),需要在将在Ubuntu平台上运行的python项目中加载。
The goal is that I have some libraries that have already been converted into .so files and now I need to load them in my python project. 目的是我已经将一些库转换为.so文件,现在我需要将它们加载到python项目中。
Can anyone share the detailed steps for this? 谁能分享详细的步骤?
You can use ctypes
for that, it's quite simple. 您可以使用ctypes
,这非常简单。
Say that you have a my-library.so
with the following C function exported: 假设您有一个my-library.so
,并且导出了以下C函数:
void say_hello(char *name) {
printf("Hello, %s!\n", name);
}
You would load the library and call the function from Python like this: 您将加载库并像这样从Python调用函数:
>>> from ctypes import cdll
>>> mylib = cdll.LoadLibrary('./my-library.so')
>>> mylib.say_hello("world")
Hello, world!
>>>
Note that the leading ./
is important, otherwise LoadLibrary
will look in the default library path and not the current folder. 请注意,前导./
很重要,否则LoadLibrary
将在默认库路径而不是当前文件夹中查找。
Fore more information refer to the documentation for ctypes
. 有关更多信息,请参阅ctypes
文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.