简体   繁体   English

C++ "Hello World" 程序调用 hello.py 到 output 字符串?

[英]C++ "Hello World" program that calls hello.py to output the string?

I want to learn how to execute Python programs from within C++ code.我想了解如何从 C++ 代码中执行 Python 程序。 How is this done, for example using the simplest of C++ and Python programs as shown below (the classic "Hello World" programs)?这是如何完成的,例如使用最简单的 C++ 和 Python 程序,如下所示(经典的“Hello World”程序)? For now I want to learn how to do this on my Windows 10 desktop where I do have Visual Studio and can code and execute C/C++/Python;现在我想学习如何在我的 Windows 10 桌面上执行此操作,我确实有 Visual Studio 并且可以编码和执行 C/C++/Python; a longer term goal will be to do this sort of thing on a Raspberry Pi.一个长期的目标是在 Raspberry Pi 上做这种事情。 Any help greatly appreciated.非常感谢任何帮助。 I have Python 3 installed on my Windows 10 PC.我的 Windows 10 PC 上安装了 Python 3。

// C++ Program
#include <iostream>
int main() {
    std::cout << "Hello World!";
    // How can the above line of code be changed so that 
    // 'Hello World!' is output using a call to a simple
    // hello.py program ?
    return 0;
}

In Visual Studio, you'll need to make sure the relevant Python include directories and libraries are linked against your C++ project.在 Visual Studio 中,您需要确保相关的 Python 包含目录和库链接到您的 C++ 项目。 First, find where Python is installed on your system;首先,找到 Python 在你的系统上安装的位置; for me it is in C:\Python39 but you may have to go on a bit of a wild goose chase if Windows has decided to bury it somewhere else.对我来说,它位于C:\Python39但如果 Windows 决定将其埋在其他地方,您可能不得不对 go 进行一些疯狂的追逐。

Make sure that your target architecture matches the Python architecture;确保您的目标架构与 Python 架构匹配; this almost certainly means making sure that you set the dropdown next to the big friendly Local Windows Debugger button to x64 .几乎可以肯定意味着确保将大型友好Local Windows Debugger按钮旁边的下拉菜单设置为x64

Next, in your Visual Studio project, lets say it's named PythonTestProject , you'll need to right click on the project name in the Solution Explorer window and click properties.接下来,在您的 Visual Studio 项目中,假设它名为PythonTestProject ,您需要在解决方案资源管理器 window 中右键单击项目名称并单击属性。 Then navigate to Configuration Properties -> C/C++ -> General and add the directory C:\Python39\include to the Addition include directories field, of course changing the path prefix to wherever you installation is.然后导航到Configuration Properties -> C/C++ -> General并将目录C:\Python39\includeAddition include directories字段,当然将路径前缀更改为您安装的位置。 Then navigate to Configuration Properties -> Linker -> Input and prepend C:\Python39\libs\python3.lib;然后导航到Configuration Properties -> Linker -> InputC:\Python39\libs\python3.lib; (note the semicolon) to the Additional dependencies field. (注意分号) Additional dependencies字段。 Your project is now good to be run against Python.您的项目现在可以针对 Python 运行。

To run a simple file, you need the PyRun_SimpleFile (the simple here doesn't actually refer to how complicated the code in the file is, as much as the simple calling convention) which is provided in Python.h .要运行一个简单的文件,您需要 Python.h 中提供的PyRun_SimpleFile (这里的简单实际上并不是指文件中的代码有多复杂,就像简单的调用约定Python.h )。 You'll also need to initialize the interpreter before calling this function, and optionally clean it up afterwards although this will be done automatically when your program ends.您还需要在调用此 function 之前初始化解释器,并且可以选择在之后清理它,尽管这将在您的程序结束时自动完成。 So something like this:所以是这样的:

#include <Python.h>
#include <stdio.h>

int main()
{
    // Initialize the Python interpreter
    Py_Initialize();

    // Open a script and run it
    const char* filename = "hello.py";
    FILE* f = fopen(filename, "rb");
    PyRun_SimpleFile(f, filename);

    // Clean up any memory and state allocated by the Python interpreter
    fclose(f);
    Py_Finalize();
}

should do the trick.应该做的伎俩。 Make sure that hello.py is in the same directory as the rest of the source files in your project, hit that big friendly debug button and it should run you file.确保hello.py与项目中源文件的 rest 位于同一目录中,点击那个友好的调试按钮,它应该会运行你的文件。

Hope this gets you on the right track:).希望这能让你走上正确的轨道:)。 I do strongly recommend looking into a C++ library such as pybind11 which will make your life much (much much) easier than dealing directly with the C API.我强烈建议您研究 C++ 库,例如pybind11 ,这将使您的生活比直接处理 C API 更容易(很多)。 If you want to just do simple things like this though, then it is not worth the trouble;但是,如果您只想做这样简单的事情,那就不值得了; the functions described in the C API documentation can do a lot of simple tasks very easily. C API 文档中描述的功能可以非常轻松地完成许多简单的任务。

On C/C++, you can use the system command to execute any shell command.在 C/C++ 上,您可以使用系统命令执行任何 shell 命令。 That would allow you to use just about any other language, including Python, Node.js, C#... docs这将允许您使用几乎任何其他语言,包括 Python、Node.js、C#... 文档

#include <cstdlib>
int main(){
    system("echo hello world");
}

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

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