c++扩展Python(未验证)

1.安装vs2017,python3;

2.创建vs空项目,不要为解决方案创建目录,添加main.cpp;

3.编译配置:release,x64;

4.设置python安装路径的include目录到附加包含目录(属性->c/c++->常规->附加包含目录);

5.设置python38.lib到附加依赖项(属性->链接器->输入->附加依赖项);

6.在main.cpp目录下添加init.py;

7.编码:Hello World

main.cpp

#include<Python.h>
//省略...

int main()
{
  //初始化Python环境
  Py_Initialize();
 
  try
  {
    int  res = 0;
    const char* filename = "init.py";
    FILE *fp = fopen(filename, "r");
     //...

    //运行init.py
   ret = PyRun_AnyFile(fp,filename);   
   //...
   fclose(fp);
  }
  catch
  {
    //.....
  }
  //释放python环境
  Py_Finalize();

  getchar();
  return 0;
}

init.py

#-*- coding:utf-8-*-

def Init()
  print ("Hello World")

Init()

 

2.通过自带库使用python

与上面基本相同。

编译python源码编译得到python37.dll和python37.lib.将.lib,.dll文件,和include文件夹,lib文件夹复制到vs工程下

posted @ 2022-01-15 13:00  _KikyoBK  Views(34)  Comments(0Edit  收藏  举报