码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pybind11使用总结(依赖python3.7版本)


    • 先安装python3.7的源码;
    • test_py.py调用test_cpp中导出函数add()及python三方库:
    1. import os
    2. import pandas as pd
    3. import numpy as np
    4. import libtest as core
    5. def test(a):
    6. print(pd.__version__)
    7. print(np.__version__)
    8. print(core.add(1,2))
    • test_cpp.cpp导出函数并调用test_py.py中函数test()
      1. #include
      2. #include
      3. #include "include/pybind11/pybind11.h"
      4. #include "test_cpp.h"
      5. int add(int i,int j){
      6. return i+j;
      7. }
      8. PYBIND11_MODULE(libtest,m){
      9. m.doc()="pybind11 example";
      10. m.def("add",&add,"add two number");
      11. }
      12. extern "c"{
      13. int todo(){
      14. pybind11::scoped_interpreter m_python;
      15. pybind11::object scope=pybind11::module::import("test_py");
      16. scope.attr("test")(10);
      17. return 0;
      18. }
      19. }

      test_cpp.h:

    1. extern "C"{
    2. int todo();
    3. }
    • 编译cmakelist.txt:

     

     linuxBuild.sh:

    执行bash linuxBuild.sh编译; 

    • test_cpp_exe.cpp为主进程,调用test_cpp.cpp中test函数:

    1. #ifdef _WIN32
    2. #include
    3. #else
    4. #include
    5. #endif
    6. #include
    7. typedef void (* todo)(void);
    8. int main(){
    9. auto m_gdllhand = dlopen("./test/Ouput/linux/Release/libtest.so",RTLD_NOW|RTLD_GLOBAL);
    10. if (m_gdllhand==nullptr){
    11. std::cout << "test..." << std::endl;
    12. return -1;
    13. }
    14. auto m_get_init = (todo)dlsym(m_gdllhand,"todo");
    15. m_get_init();
    16. return 0;
    17. }

    编译执行:

    g++ test_cpp_exe.cpp -o test_cpp_exe -l test -l dl -L ./ 

    最后运行脚本run.sh:

    1. #!/bin/bash
    2. pwd_path=`pwd`
    3. export LD_LIBRARY_PATH=${pwd_path}:$LD_LIBRARY_PATH
    4. ./test_cpp_exe

    执行结果:

     

  • 相关阅读:
    AI辅助研发,引领科技新潮流
    驱动开发,udev机制创建设备节点的过程分析
    为什么蛋白质如此重要?它可以帮助你自然地减肥!
    信安软考——第六章 认证技术原理和应用 笔记记录
    【李沐深度学习笔记】矩阵计算(1)
    图像检索常规算法---学习笔记
    java计算机毕业设计航空订票管理系统源程序+mysql+系统+lw文档+远程调试
    宇宙最強的IDE - Visual Studio 25岁生日快乐
    广东省科学技术厅关于2023年度广东省科学技术奖提名工作的通知
    决策树算法、随机森林算法
  • 原文地址:https://blog.csdn.net/weixin_42446330/article/details/127892125
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号