准备:
- 参看 https://editor.csdn.net/md/?articleId=105933925
配置基本的c++ 环境
第一步: 将空文件夹添加到 工作目录中
第二步: 编写c++ main.cpp函数测试c++ 环境正常
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a = 521;
int b = 125;
int c = a + b;
cout << "Standard exception: " <<c<<endl;
}
ctrl+shitf+b 进行编译
右键 runcode
这样说明c++ 环境完全没有问题
下面开始嵌入python环境
包含Python中的include
按下ctrl + shift + p 输入 c/c++
配置: c++ includePath
加入 你的python 安装路径 下的 include
将Python.h包含到程序当中
run 一下
这个真是无解, 谁知道告诉博主一下,
这里给出一个测试了好多遍的 解决方案, 有点蠢 勿喷
就是 使用 绝对路径
run 一下 没报错了 笑哭
下面先整一个, 简单的 调用python语句
#include <stdio.h>
#include<iostream>
#include "D:\\Program Files\\python3\\include\\Python.h"
using namespace std;
int main()
{
int a = 521;
int b = 125;
int c = a + b;
cout << "Standard exception: " <<c<<endl;
// python is here
Py_SetPythonHome(L"D:\\Program Files\\python3");
Py_Initialize(); //初始化python解释器,告诉编译器要用的python编译器
PyRun_SimpleString("print('hello word! my name is python')"); //调用python文件
Py_Finalize(); //结束python解释器,释放资源
}
尝试运行一下
ctrl + shit + b 运行之前先配置一下 task
加入这一句:
解释一下为什么这样做:
因为这会儿, 咱们使用c++ 来调用python ,需要链接python 的静态库
这句话,根据自己的情况添加
提醒一下, 每次修改 顺手 按下 ctrl + s , 保存一下 才会哦生效
再次按下 ctrl + shift + b 进行编译,
我的结果如下:
尝试运行一下:
那么这样一个 python 代码 就实现了
下面整个 复杂一点的,调用python 文件中的函数
待更新,未完