vscode内c++调用python内函数


VScode中配置环境

首先是在VScode中为C++调用python接口配置环境

1、配置step1

打开vscode的拓展商店安装python
在这里插入图片描述

2、配置step2

然后编辑c_cpp_properties.json文件,在文件中的includePath项添加自己的python include路径

{
    "configurations": [
        {
          "name": "Win32",
          "includePath": [
            "${workspaceFolder}/**",
            "E:\\Anaconda3\\include"#这里的路径即为我们要添加的自己的python路径
          ],
          "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
          "windowsSdkVersion": "10.0.17763.0",
          "compilerPath": "E:\\C++\\mingw64\\bin\\g++.exe",   /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
          "cStandard": "c11",
          "cppStandard": "c++17",
          "intelliSenseMode": "${default}"
        }
      ],
      "version": 4
}

3、配置step3

编辑jasks.json文件,

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "task g++",
            "command": "E:\\C++\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-L","E:\\Anaconda3\\libs\\*",#这里改为自己python库文件夹路径
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "E:\\Anaconda3\\include",#这里改为自己python的include路径
                "-std=c++17"
            ],
            "options": {
                "cwd": "E:\\C++\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: cpp.exe 生成活动文件",
            "command": "E:\\C++\\mingw64\\bin\\cpp.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}

调用python

python测试代码

test.py

def hello():
    print("hello")

main.cpp

#include "E:\\Anaconda3\\include\\Python.h"
//#include "Python.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main()
{

    Py_SetPythonHome(L"E:\\Anaconda3");
    Py_Initialize();                          //初始化python解释器,告诉编译器要用的python编译器
    PyRun_SimpleString("import test");       //调用python文件
    PyRun_SimpleString("hell.hello()"); //调用上述文件中的函数
    Py_Finalize();                      //结束python解释器,释放资源
    system("Pause");
    return 0;                 
}

结果

在这里插入图片描述

参考文章

在VS2019和VScode中配置C++调用python接口
Vscode 使用 C++ 调用python
VsCode安装和配置c/c++环境(超完整,小白专用)

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值