python call clib

touch 2 files in the same dir

1.c_func.c

1 #include <stdio.h>
2 
3 int add(int a,int b)
4 {
5     printf("var1 = %d,var2 = %d\n",a,b);
6 
7     return a + b;
8 }

 

gcc -o libc_func.so -shared -fPIC c_func.c

 

2.pycallc.py

1 #!/usr/bin/python3
2 
3 import ctypes
4 
5 loadLibrary = ctypes.cdll.LoadLibrary;
6 cLib = loadLibrary("./libc_func.so");
7 ret = cLib.add(250 , 666);
8 print("res = %d"%ret);

 

posted @ 2020-12-29 23:21  蓝慕凡  阅读(101)  评论(0)    收藏  举报