CPython开发全过程

本文详述了在Debian12系统上使用CPython进行程序开发的全过程,包括安装依赖、配置VSCode远程、C++调用Python脚本的实现及调试。在调试过程中,遇到并解决了如找不到Python头文件、g++编译错误等问题,通过添加适当参数解决编译和运行问题。同时,文章探讨了C++和Python互相调用的可能性。
摘要由CSDN通过智能技术生成

说明

  • 操作系统: debian12
  • 安装apt install gcc
  • 安装 apt install python3 -dev
  • 安装 apt install g++

配置vscode远程

  • 安装ssh-development
  • 配置 .ssh文件
Host 192.168.225.111
  HostName 192.168.225.111
  User rc
  IdentityFile E:\vm_debian\rc_rsa
  • 报错信息:
    • 1.It is required that your private key files are NOT accessible by others
      • 比较麻烦,暂时还没有找打比较好的方法,先暂时使用密码登录方便,后续有空了再研究
      • 参考链接: https://zhuanlan.zhihu.com/p/97706106

程序设计实现

  • 编译脚本
    • g++ -o bubble bubble.cpp -I /usr/include/python3.11 -l python3.11 不能使用gdb调试
    • g++ -g bubble.cpp -o bubble -I /usr/include/python3.11 -l python3.11 可以使用gdb调试
      • -g 参数可以gcc在编译是会做以下额外的操作:
        • 创建符号表,符号表包含了程序中使用的变量名称的列表
        • 关闭所有的优化机制,以便程序执行过程中严格按照原来的C代码进行
c++ 执行python的脚本
  • 参考链接:https://zhuanlan.zhihu.com/p/79896193
// 第一版可以执行成功
#include <Python.h>

// 编译方法:g++ -o bubble bubble.cpp -I /usr/include/python3.11 -l python3.11 

int main() {
   
    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("print('hello world')");
    Py_Finalize();
    return 0;
}
  • 第二版:
#include <Python.h>
#include <iostream>

using namespace std;

// 编译方法:gcc -o bubble bubble.cpp -I /usr/include/python3.11 -l python3.11 

int main() {
   
    Py_Initialize();
    if (!Py_IsInitialized())
    {
   
        cout << "Python init fail" << endl;
        return -1;
    }
    cout << "Python init ok" << endl;
    

    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");  // 必须指定该目录,负责会执行失败
    // PyRun_SimpleString("print('hello world')");
    PyObject* pModule = PyImport_ImportModule("bubble_sort");
    if (pModule == NULL)
    {
   
        cout << "PyImport_ImportModule fail" << endl;
        return -1;
    }

    PyObject* pFunc = PyObject_GetAttrString(pModule, "say_hello");
    if (!pFunc || !PyCallable_Check(pFunc))
    {
   
        cout << "not found function say_hello" << endl;
        return -1;
    }

    PyObject_CallObject(pFunc, NULL);

    Py_Finalize();
    return 0;
}
  • 第三版
#include <Python.h>
#include <iostream>

using namespace std;

// 编译方法:gcc -o bubble bubble.cpp -I /usr/include/python3.11 -l python3.11 

int main() {
   
    Py_Initialize();
    if (!Py_IsInitialized())
    {
   
        cout << "Python init fail" << endl;
        return -1;
    }
    cout << "Python init ok" << endl;
    

    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");
    // PyRun_SimpleString("print('hello world')");
    PyObject* pModule = PyImport_ImportModule("bubble_sort");
    if (pModule == NULL)
    {
   
        cout << "PyImport_ImportModule fail" << endl;
        return -1;
    }

    PyObject* pFunc = PyObject_GetAttrString(pModule, "bubble_sort");
    if (!pFunc || !PyCallable_Check(pFunc))
    {
   
        cout << "not found function bubble_sort" << endl;
        return -1;
    }

    PyObject* pArgs = PyList_New(0);  // 初始化一个空列表
    PyList_Append(pArgs, Py_BuildValue("i", 10));
    PyList_Append(pArgs, Py_BuildValue("i", 99));
    PyList_Append(pArgs, Py_BuildValue("i", 8));
    PyList_Append(pArgs, Py_BuildValue("i", 71));
    PyList_Append(pArgs, Py_BuildValue("i", 6));
    PyList_Append(pArgs, Py_BuildValue("i", 53
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值