1、 C 代码
1.1 test.c
#include<stdio.h> #include<stdlib.h> #include"test.h" int hello() { printf("hello word 666\n"); return 255; }
1.2 test.h
#include<stdio.h> #include<stdlib.h> int hello();
1.3 调试用的main.cpp
#include"test.h" int main() { hello(); hello(); }
2、将test.c 里面的函数封成.so
gcc -fPIC -shared test.c -o libtest.so
3、python 调库
import os from ctypes import * from ctypes import CDLL,c_int,c_void_p so_path= os.getcwd()+'/libtest.so' print(so_path) c_functin=CDLL(so_path) hello=c_functin.hello hello() hello() hello()
4、目录结构
5、运行结果