<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>python on 施国鹏 (seth-shi)</title><link>https://sethix.com/zh-cn/tags/python/</link><description>Recent content in python on 施国鹏 (seth-shi)</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Fri, 28 Mar 2025 21:31:03 +0800</lastBuildDate><atom:link href="https://sethix.com/zh-cn/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>python 调用 C++ 动态库文件</title><link>https://sethix.com/zh-cn/posts/2025032821/</link><pubDate>Fri, 28 Mar 2025 21:31:03 +0800</pubDate><guid>https://sethix.com/zh-cn/posts/2025032821/</guid><description> 最近写python项目的时候要调用C++的动态库文件，python的ctypes库和pybind11库都可以调用动态库文件 ctypes 简单类型就用ctypes,python原生自带, 使用起来简单 但是C++有复杂类型, 比如vector时, 就会报错Segmentation fault (core dumped), 因为ctypes没有vector的类型映射 https://stackoverflow.com/questions/49744870/passing-vector-from-a-c-dll-in-python-ctypes pybind11 pybind11是一个可以将C++的代码编译成python的动态库文件https://github.com/pybind/pybind11 让C++的同事打包好so文件给你, 只需要so文件就行 需要注意的点 build环境的python版本要保持和你运行环境一样 打包出来的文件名如下xxxx.cpython-311(版本名)-x86_64(CPU架构)-linux-gnu(操作系统).so 找到自己的的项目目录放进去, 比如app/libs/xxx/xxxx.so 记得执行export PYTHONPATH=&amp;quot;$PYTHONPATH:/var/www/app/libs/xxx&amp;quot;(否则就会报错xxx No module named) 类型提示 安装pip install pybind11-stubgen 在so目录下执行pybind11-stubgen xxx -o . 后面其它地方调用,直接使用站桩代码调用就可以</description></item></channel></rss>