Interaction between Python and C

        As a scripting language, python has the advantage of simple syntax, many things have been packaged, and you can just use it directly. Therefore, to achieve the same function, writing in Python requires much less code than using C/C++. But the advantages are bound to be accompanied by disadvantages (this is for sure, otherwise why do you need other languages), one of the most criticized places in python may be its running speed. This is a common problem faced by most scripting languages. Because there is no compilation process, it is directly executed line by line, so it is a lot slower. Therefore, in some occasions that require high speed, it is generally written in a compiled language such as C/C++.

1 python

#!/usr/bin/python3
import os
import ctypes
from ctypes import *

dir = os.getcwd()
file2 = os.path.join(dir, "libtest.so")
libtest = cdll.LoadLibrary(file2)

libtest.test()

# 传递字符数组
temp = c_char * 10
buffer = temp()
for i in range(10):
    buffer[i] = b'a'
res = libtest.print_str(pointer(buffer))

# 传递整形、整形指针
buffer=10
a = c_int(buffer)
buffer=20
b = c_int(buffer)
libtest.add(a, b)
libtest.add_p(pointer(a), pointer(b))


# 传递结构体、结构体指针
class user_struct_def(Structure):
    _fields_ = [('param1', c_int), ('param2', c_int), ('param3', c_char * 20)]


user_struct = user_struct_def()
user_struct.param1 = 1
user_struct.param2 = 1
user_struct.param3 = b'hello world'
libtest.test2(pointer(user_struct))
libtest.test3(user_struct)

# python 调用c函数默认返回值为int 其他类型需转化
a = c_float(12.7)
b = c_float(2.2)
libtest.test4.restype = ctypes.c_float
res = libtest.test4(a, pointer(b))
print(res)

# python 中数据转化为c语言数据(使用c_float() C_short()类型转化)
a = 22400.1
a = a + 1
temp1 = c_float(a)

b = 10.5
b = b + 1
temp2 = c_float(b)
libtest.test4.restype = ctypes.c_float
res = libtest.test4(temp1, pointer(temp2))
print(res)

# python 元组 列表 字符串转c数组
a = (1, 2, 3, 4, 5, 6, 7, 8)
temp = c_int * 20
buffer = temp()
for i in range(len(a)):
    buffer[i] = c_int(a[i])

temp = "hello world"
a = bytes(temp, "utf-8")
print(a)
temp = c_char * 20
buffer = temp()
for i in range(len(a)):
    buffer[i] = a[i]
res = libtest.print_str(pointer(buffer))

2 c language

#include "stdio.h"

struct user_struct_def
{
    int a;
    int b;
    char c[20];
};

void test(void)
{
	printf("test\r\n");
}

int print_str(unsigned char *p)
{
	printf("print_str=%s\r\n",p);
	return 1;
}

int add(int a, int b)
{
	int ret;
	ret=a+b;
	printf("add=a+b=%d\r\n",ret);
	return ret;
}

int add_p(int *a,int *b)
{
	int c=0;
	c=*a+*b;
	printf("add_p=%d\r\n",c);
	return c;
}

int test2(struct user_struct_def *user_struct)
{
    printf("user_struct.a=%d\r\n",user_struct->a);
    printf("user_struct.b=%d\r\n",user_struct->b);
    printf("user_struct.c=%s\r\n",user_struct->c);
    return 1;
}


int test3(struct user_struct_def user_struct)
{
    printf("user_struct.a=%d\r\n",user_struct.a);
    printf("user_struct.b=%d\r\n",user_struct.b);
    printf("user_struct.c=%s\r\n",user_struct.c);
    return 1;
}

float test4(float a,float*b)
{
    float res;
    res=a+*b;
    return res;
}

Guess you like

Origin blog.csdn.net/w356877795/article/details/120062228