栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

vscode内c++调用python内函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vscode内c++调用python内函数

vscode内c++调用python内函数
  • VScode中配置环境
    • 首先是在VScode中为C++调用python接口配置环境
    • 1、配置step1
    • 2、配置step2
    • 3、配置step3
  • 调用python
    • python测试代码
  • 结果
  • 参考文章


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

打开vscode的拓展商店安装python

2、配置step2

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

{
    "configurations": [
        {
          "name": "Win32",
          "includePath": [
            "${workspaceFolder}
          "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 
#include 
#include 
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++环境(超完整,小白专用)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847145.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号