python ctypes调用c++ so库;python setuptools工具库打pypi包

本文介绍了如何使用Python通过ctypes调用C++的SO库,重点展示了如何在C++接口上层用C语言封装以便于跨语言调用,并演示了如何利用setuptools工具将Python包打包成可安装模块。实例包括编译动态库、参数映射和编码问题处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://blog.csdn.net/weixin_42357472/article/details/117533537

https://tech.meituan.com/2022/04/21/cross-language-call.html
https://www.jianshu.com/p/346ac40ac2c3

1、python调用c++ so库

1、str_print.h

#pragma once
#include <string>
class StrPrint {
 public:
    void print(const std::string& text);
};

2、str_print.cpp

#include <iostream>
#include "str_print.h"
void StrPrint::print(const std::string& text) {
    std::cout << text << std::endl;
}

3、c_wrapper.cpp
Python、Java支持调用C接口,但不支持调用C++接口,因此对于C++语言实现的接口,必须转换为C语言实现。为了不修改原始C++代码,在C++接口上层用C语言进行一次封装

#include "str_print.h"
extern "C" {
void str_print(const char* text) {
    StrPrint cpp_ins;
    std::string str = text;
    cpp_ins.print(str);
}
}

4、编译生成动态库:

g++ -o libstr_print.so str_print.cpp c_wrapper.cpp -fPIC -shared

在这里插入图片描述
5、python调用案例

# -*- coding: utf-8 -*-
import ctypes
# 加载 C lib
lib = ctypes.cdll.LoadLibrary("./libstr_print.so")
# 接口参数类型映射
lib.str_print.argtypes = [ctypes.c_char_p]
lib.str_print.restype = None
# 调用接口
lib.str_print(b'Hello World')

在这里插入图片描述
***传递中文
‘aaHello 你好’.encode(‘gbk’)

# -*- coding: utf-8 -*-
import ctypes
# 加载 C lib
lib = ctypes.cdll.LoadLibrary("./libstr_print.so")
#接口参数类型映射
# lib.str_print.argtypes = [ctypes.c_char_p]
# lib.str_print.restype = None
# 调用接口
bb = lib.str_print('aaHello 你好'.encode('utf-8'))

bb = lib.str_print('aaHello 你好'.encode('gbk'))

在这里插入图片描述

2、python setuptools工具库打包python包

参考:https://blog.csdn.net/qq_41134008/article/details/105574136
https://blog.csdn.net/ns2250225/article/details/45559631

在这里插入图片描述
*创建箭头这4个文件结构,其他是python setup.py install 后生成的
在这里插入图片描述
1、init.py

# -*- coding: utf-8 -*-
import ctypes
 import os
 import sys
 dirname, _ = os.path.split(os.path.abspath(__file__))
 lib = ctypes.cdll.LoadLibrary(dirname + "/libstr_print.so")
 lib.str_print.argtypes = [ctypes.c_char_p]
 lib.str_print.restype = None
 def str_print(text):
     lib.str_print(text)

2、setup.py

from setuptools import setup, find_packages
setup(
    name="strrprint",
    version="1.0.0",
    packages=find_packages(),
    include_package_data=True,
    description='str print',
    author='xxx',
    package_data={
        'strprint': ['*.so']
    },
)

3、MANIFEST.in

include strprrint/libstr_print.so

4、打包安装

python setup.py install 

5、引用加载包测试

# -*- coding: utf-8 -*-
import sys
from strrprint import *
str_print(b'Hello py')

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

loong_XL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值