找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 1190|回复: 1

Python3调用C++代码之初试

[复制链接]

307

主题

228

回帖

7319

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5569 个
贡献
253 次
宅之契约
0 份
在线时间
945 小时
注册时间
2014-1-25
发表于 2021-9-19 15:22:42 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×

Python3调用C++代码之初试

前言

  因业务需求,需要Python调用C++开发的模块,尝试可行的方案有pybind11,cython,boost.python,swig,下面一一列举用法

环境准备

  • MacOS
  • Python3
  • python模块pybind11/cython
  • boost/swig
pip3 install pybind11 cython --user
brew install boost swig

pybind11

编写C++代码

// test.cpp
#include <iostream>
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(testpybind11, m) {
    m.doc() = "pybind11 example plugin"; // 导出函数cpp_function
    m.def("cpp_function", [](const std::string& s1) {
        std::cout << s1 << std::endl;
        return s1;
    });
}
// 

编译兼容模块

c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup $(python3 -m pybind11 --includes) -DPYBIND11 test.cpp -o testpybind11$(python3-config --extension-suffix)
# 笔者Mac上生成testpybind11.cpython-39-darwin.so

python测试

import testpybind11
out = testpybind11.cpp_function("hello")
print(out)

cython

编写C++及接口代码

// test.cpp
#define EXPORT __attribute__ ((visibility("default")))
#include <iostream>
EXPORT std::string testcython(const std::string& s1) {
    std::cout << s1 << std::endl;
    return s1;
}

// testcython.pyx
from libcpp.string cimport string

cdef extern from "test.cpp":
    string testcython(const string& s1)
def pytestcython(const string& s1):
    return testcython(s1)

编译兼容模块

cython --cplus testcython.pyx -o test_wrapper.cpp
c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup $(python3 -m pybind11 --includes) -DPYTHON test_wrapper.cpp -o testcython$(python3-config --extension-suffix)

python测试

import testcython
input_str = "hello"
out = testcython.pytestcython(input_str.encode())
output_str = out.decode()
print(output_str)

boost.python

编写C++代码

// test.cpp
#include <iostream>
#include <boost/python.hpp>
std::string testpyboost(const std::string& s1) {
    std::cout << s1 << std::endl;
    return s1;
}
BOOST_PYTHON_MODULE(testpyboost)
{
    boost::python::def("cpp_function", testpyboost);
}

编译兼容模块

c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup $(python3 -m pybind11 --includes) -DPYBOOST test.cpp -o testpyboost$(python3-config --extension-suffix)

python测试

import testpyboost
out = testpyboost.cpp_function("hello")
print(out)

swig

编写C++及接口代码

// test.cpp
#define EXPORT __attribute__ ((visibility("default")))
#include <iostream>
EXPORT std::string pytestswig(const std::string& s1) {
    std::cout << s1 << std::endl;
    return s1;
}

// test.i
%module testswig
%include  "std_string.i"
%{
    std::string pytestswig(const std::string& s1);
%}
std::string pytestswig(const std::string& s1);

编译兼容模块

swig -python -c++ test.i
c++ -O3 -Wall -shared -undefined dynamic_lookup $(python3 -m pybind11 --includes) test_wrap.cxx test.cpp -o _testswig$(python3-config --extension-suffix)

python测试

import testswig
out = testswig.pytestswig("hello")
print(out)
回复

使用道具 举报

1

主题

60

回帖

333

积分

用户组: 中·技术宅

UID
6035
精华
0
威望
2 点
宅币
266 个
贡献
0 次
宅之契约
0 份
在线时间
29 小时
注册时间
2020-7-7
发表于 2021-12-17 14:10:43 | 显示全部楼层
很好的方法,值得学习
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-3-29 03:02 , Processed in 0.047827 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表