1、test.c
#include <stdio.h>
void test(){
printf("testing....");
}
int main(int argc, char **argv) {
test();
return 0;
}
2、将test.c文件输入为动态库,取名为libtest.so
gcc test.c -shared -fPIC -o libtest.so
// 如果少了 -share 提示,cannot dynamically load executable
// -fPIC是产生位置无关代码,就是可以不固定的内存位置执行代码(就是可以动态链接的意思了),动态链接库必须添加。
3、main.py
from ctypes import *
from threading import Thread
# 加载动态库
lib = cdll.loadLibrary("./libtest.so")
# 创建子进程,让其执行c函数
t = Thread(target=lib.test)
t.start()
# 主线程
while True:
pass
4、python main.py
输出 :testing....