python调用C/C++

python调用C/C++

简单C函数(无参数无返回值)

1 编写C程序

// sample.c
#include <stdio.h>

void hello_world() 
{
    printf("Hello, world!\n");
}

2 编译C程序

gcc -shared -o libsample.so sample.c

3 编写Python程序

# test.py
import ctypes

# 加载共享库
lib = ctypes.cdll.LoadLibrary('./libsample.so')

# 调用函数
lib.hello_world()

简单C函数(简单参数)

1 编写C程序

/* sample.c */
int add(int a, int b) {
    return a + b;
}

2 编译C程序

gcc -shared -o libsample.so sample.c

3 编写Python程序

# test.py
import ctypes

# 加载共享库
lib = ctypes.cdll.LoadLibrary('./libsample.so')

# 调用C语言函数
result = lib.add(1, 2)

print(result)  # 输出 3

C函数(结构体参数)

1 编写C程序

/* sample.c */
#include <stdio.h>

struct Point {
    double x;
    double y;
};

void print_point(struct Point p) {
    printf("(%f, %f)\n", p.x, p.y);
}

2 编译C程序

gcc -shared -o libsample.so sample.c

3 编写Python程序

# example.py
import ctypes

# 定义结构体
class Point(ctypes.Structure):
    _fields_ = [("x", ctypes.c_double),
                ("y", ctypes.c_double)]

# 加载库文件
lib = ctypes.CDLL("./libsample.so")

# 获取函数
print_point = lib.print_point
print_point.argtypes = [Point]
print_point.restype = None

# 创建结构体实例并调用函数
p = Point(1.0, 2.0)
print_point(p)

C函数(结构体变量赋值)

1 编写C程序

typedef struct {
    int age;
    char *name;
} Person;

2 编译C程序

gcc -shared -o libsample.so sample.c

3 编写Python程序

import ctypes

class Person(ctypes.Structure):
    _fields_ = [("age", ctypes.c_int),
                ("name", ctypes.c_char_p)]
    
    
my_struct = Person()
my_struct_ptr = ctypes.pointer(my_struct)

my_struct_ptr.contents.age = 25
my_struct_ptr.contents.name = b"John"

C函数(枚举参数)

1 编写C程序

#include <stdio.h>

typedef enum {
    SUCCESS,
    ERROR
} ReturnCode;

void print_return_code(ReturnCode code)
 {
    switch (code) {
        case SUCCESS:
            printf("Success\n");
            break;
        case ERROR:
            printf("Error\n");
            break;
        default:
            printf("Unknown code\n");
    }
}

2 编译C程序

gcc -shared -o example.so example.c

3 编写Python程序

import ctypes

# 加载动态库
lib = ctypes.CDLL('./example.so')

# 定义 ReturnCode 枚举类型
class ReturnCode(ctypes.c_int):
    SUCCESS = 0
    ERROR = 1

# 定义函数参数类型和返回值类型
lib.print_return_code.argtypes = [ReturnCode]
lib.print_return_code.restype = None

# 调用函数
lib.print_return_code(ReturnCode.SUCCESS)
lib.print_return_code(ReturnCode.ERROR)

C++类函数

1 编写C++程序

#include <iostream>

class Person {
public:
    Person(const char* name) : m_name(name) {}
    void say_hello() { std::cout << "Hello, I'm " << m_name << std::endl; }
private:
    const char* m_name;
};

extern "C" {
    Person* create_person(const char* name) {
        return new Person(name);
    }

    void delete_person(Person* person) {
        delete person;
    }

    void say_hello(Person* person) {
        person->say_hello();
    }
}

2 编译C程序

gcc -shared -o libsample.so sample.c

3 编写Python程序

import ctypes

# 加载 C++ 动态链接库
lib = ctypes.CDLL('./libexample.so')

# 定义 Person 类型
class Person(ctypes.Structure):
    pass
PersonPtr = ctypes.POINTER(Person)

# 定义 C++ 函数返回值类型
lib.create_person.restype = PersonPtr

# 定义 C++ 函数参数类型
lib.create_person.argtypes = [ctypes.c_char_p]
lib.delete_person.argtypes = [PersonPtr]
lib.say_hello.argtypes = [PersonPtr]

# 创建 Person 实例
person = lib.create_person(b'John')

# 调用 say_hello 函数
lib.say_hello(person)

# 销毁 Person 实例
lib.delete_person(person)
posted @   Truman001  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示