c++扩展Python(未验证)(C + + extension python (unverified))

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()
————————

1. Install vs2017, python 3;

2. Create vs empty project, do not create directory for solution, add main cpp;

3. Compilation configuration: release, x64;

4. Set the include directory of the python installation path to the additional include directory (attribute – & gt; C / C + + – & gt; general – & gt; additional include directory);

5. Set Python 38 Lib to attach dependency (attribute – & gt; linker – & gt; input – & gt; attach dependency);

6. In main Add init. In CPP directory 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()