Python調(diào)用C語(yǔ)言開(kāi)發(fā)的共享庫(kù)方法實(shí)例
在helloworld工程中,編寫了一個(gè)簡(jiǎn)單的兩個(gè)數(shù)值相加的程序,編譯成為共享庫(kù)后,如何使用python對(duì)其進(jìn)行調(diào)用呢?
使用ll命令列出當(dāng)前目錄下的共享庫(kù),其中共享庫(kù)名為libhelloworld.so.0.0.0
ufo@ufo:~/helloworld/.libs$ ll
總用量 32
drwxr-xr-x 2 ufo ufo 4096 1月 29 14:54 ./
drwxr-xr-x 6 ufo ufo 4096 1月 29 16:08 ../
-rw-r--r-- 1 ufo ufo 3816 1月 29 14:54 helloworld.o
-rw-r--r-- 1 ufo ufo 3956 1月 29 14:54 libhelloworld.a
lrwxrwxrwx 1 ufo ufo 19 1月 29 14:54 libhelloworld.la -> ../libhelloworld.la
-rw-r--r-- 1 ufo ufo 983 1月 29 14:54 libhelloworld.lai
lrwxrwxrwx 1 ufo ufo 22 1月 29 14:54 libhelloworld.so -> libhelloworld.so.0.0.0*
lrwxrwxrwx 1 ufo ufo 22 1月 29 14:54 libhelloworld.so.0 -> libhelloworld.so.0.0.0*
-rwxr-xr-x 1 ufo ufo 9038 1月 29 14:54 libhelloworld.so.0.0.0*
進(jìn)入python的命令行模式進(jìn)行C語(yǔ)言實(shí)現(xiàn)的兩個(gè)數(shù)值相加的程序的調(diào)用;
ufo@ufo:~/helloworld/.libs$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:56)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
載入ctypes類(此類即是調(diào)用C語(yǔ)言動(dòng)態(tài)庫(kù)的方法)
>>> import ctypes
打開(kāi)當(dāng)前目錄的動(dòng)態(tài)庫(kù)
>>> lib=ctypes.cdll.LoadLibrary("./libhelloworld.so.0.0.0")
調(diào)用動(dòng)態(tài)庫(kù)中的接口
>>> lib.add(5,7)
12
兩個(gè)參數(shù)的相加的函數(shù)如下:
ufo@ufo:~/helloworld$ cat helloworld.c
#include <stdio.h>
#include <stdlib.h>
int add(int a, int b)
{
int c = a + b;
return c;
}
編譯動(dòng)態(tài)庫(kù)的命令行:
gcc -shared -fPIC -DPIC helloworld.c -o libhelloworld.so.0.0.0
相關(guān)文章
Python3通過(guò)字符串訪問(wèn)和修改局部變量的方法實(shí)例
最近在看python中nonlocal和global的使用,參考網(wǎng)上的大作,寫了點(diǎn)自己的心得,下面這篇文章主要給大家介紹了關(guān)于Python3通過(guò)字符串訪問(wèn)和修改局部變量的相關(guān)資料,需要的朋友可以參考下2022-04-04
使用Python中OpenCV和深度學(xué)習(xí)進(jìn)行全面嵌套邊緣檢測(cè)
這篇文章主要介紹了使用Python中OpenCV和深度學(xué)習(xí)進(jìn)行全面嵌套邊緣檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
僅用500行Python代碼實(shí)現(xiàn)一個(gè)英文解析器的教程
這篇文章主要介紹了僅用500行Python代碼實(shí)現(xiàn)一個(gè)英文解析器的教程,自然語(yǔ)言處理近來(lái)也是業(yè)界中一個(gè)熱門課題,作者為NLP方向的開(kāi)發(fā)者,需要的朋友可以參考下2015-04-04
python3實(shí)現(xiàn)小球轉(zhuǎn)動(dòng)抽獎(jiǎng)小游戲
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)小球轉(zhuǎn)動(dòng)抽獎(jiǎng)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
用實(shí)例分析Python中method的參數(shù)傳遞過(guò)程
這篇文章主要介紹了用實(shí)例分析Python中method的參數(shù)傳遞過(guò)程,包括instancemethod和staticmethod等實(shí)例,需要的朋友可以參考下2015-04-04
使用pyinstaller打包.exe文件的詳細(xì)教程
PyInstaller是一個(gè)跨平臺(tái)的Python應(yīng)用打包工具,能夠把 Python 腳本及其所在的 Python 解釋器打包成可執(zhí)行文件,下面這篇文章主要給大家介紹了關(guān)于使用pyinstaller打包.exe文件的相關(guān)資料,需要的朋友可以參考下2022-04-04

