首先新建了test_a.c ,test_b.c ,test_c.c三個.c文件與so_test.h頭文件,編寫好test_a.c ,test_b.c ,test_c.c中具體函數后,在so_test.h中聲明test_a.c ,test_b.c ,test_c.c中具體函數,然后再linux的shell終端進行編譯
gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtestx.so
生成libtestx.so文件,然后ls查看全部文件
tom@ubuntu:~$ ls cnm Downloads juzhen1.py Pictures Templates test_c.c cnm.tar.gz examples.desktop libtest.so Public test Videos Desktop foo.txt libtestx.so shell.sh test_a.c diaoyong.py groups.txt main so_test.h test_b.c Documents haha.txt Music src test.c
已經生成了libtestx.so文件
在同一目錄下新建diaoyong.py輸入代碼如下
from ctypes import cdll cur = cdll.LoadLibrary('libtest.so') cur.test_a() cur.test_b() cur.test_c() print 'hahahaha'
然后保存退出,即可與運行diaoyong.py
結果為
This is test_aThis is test bThis is test_chahahaha
完成python調用C動態函數庫