python调用c代码

1、 C 代码
1.1 test.c

#include
#include
#include"test.h"
int hello()
{

printf("hello word 666\n");
return 255;

}

1.2 test.h

#include
#include
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、目录结构
python调用c代码_第1张图片
5、运行结果

在这里插入图片描述

你可能感兴趣的:(so,封库,c语言,开发语言)