Python 运行C语言代码

1、C语言源码(main.c)

#include <stdio.h>

void test_loop()
{
    for(int i=0; i<100; i++)
    {
        printf("C language: %d\n", i);
    }
}

int main(){
    printf("hello\n");
    test_loop();
    // getchar();
    return 0;
    // gcc -shared -o main.dll main.c
}

2、将main.c编译生成main.dll(略)

3、编写python代码运行C语言代码

from ctypes import *

""" 加载动态库 """
lib = cdll.LoadLibrary('./main.dll')
lib.test_loop()  # 调用main.dll中的 test_loop 函数

"""
还可以使用多线程

from ctypes import *
from threading import Thread

# 加载动态库
lib = cdll.LoadLibrary(r'./main.dll')

# 创建一个子线程,让其执行C语言编写的函数
t = Thread(target=lib.test_loop)
t.start()


for i in range(10):
    print('python次数: ', i)
"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值