简体   繁体   English

python 来自 Visual Studio 中的 c++

[英]python from c++ in visual studio

I used the visual studio installer to install python and could run the basic python program.我使用 Visual Studio 安装程序安装 python 并可以运行基本的 python 程序。 Now I want to make a new c++ console program and call the python script from there.现在我想制作一个新的 c++ 控制台程序并从那里调用 python 脚本。

When googling I saw I need to include the "Python.h" in my c++ function.谷歌搜索时,我发现我需要在 c++ function 中包含“Python.h”。 I tried to include and i get the message "cannot open the source file".我尝试包含并收到消息“无法打开源文件”。 Is it because I used visual studio to install the python.是不是因为我用Visual Studio安装了python。 And most of googling have installed python separately.而且大多数谷歌搜索都单独安装了 python 。

Older versions of visual studio, just installed python interpretter.旧版本的visual studio,只安装了python解释器。 So installed Visual studio 2019 and had a option to select Python3因此安装了 Visual Studio 2019 并选择了 select Python3

With that option, it created a new directory, C:\Python27amd64\使用该选项,它创建了一个新目录 C:\Python27amd64\

Now I created a new console application: Code:现在我创建了一个新的控制台应用程序:代码:

#include <iostream>
#include "C:\Python27amd64\include\Python.h"

    int main()
    {
        Py_Initialize();
        PyRun_SimpleString("from time import time,ctime\n"
         "print('Today is',ctime(time()))\n");
        //std::cout << "Hello World!\n";
    }

And also followed the following steps:并且还遵循了以下步骤:

  1. Properties > C/C++ > General > Additional Include Directories.属性 > C/C++ > 常规 > 附加包含目录。 I added the "C:\Python27amd64\include" 2.Properties > Linker > General > Additional Library Directories.我添加了“C:\Python27amd64\include”2.Properties > Linker > General > Additional Library Directories。 : I added C:\Python27amd64\libs 3.Properties > Linker > Input > Additional Dependencies: I added python27.lib :我添加了 C:\Python27amd64\libs 3.Properties > Linker > Input > Additional Dependencies:我添加了 python27.lib
  2. I changed the build to "Release x64".我将构建更改为“发布 x64”。 Because it couldnot find the debug libraries因为它找不到调试库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM