python调用c代码

2022年12月10日07:57:15

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、目录结构
python调用c代码
5、运行结果

python调用c代码

  • 作者:绕灵儿
  • 原文链接:https://blog.csdn.net/qq_36784503/article/details/127906137
    更新时间:2022年12月10日07:57:15 ,共 471 字。