在C ++中嵌入Python [英] Embedding Python in C++

查看:154
本文介绍了在C ++中嵌入Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,先生,



我已经推荐过这篇文章,在C / C ++代码中嵌入Python程序 [ ^ ]我已经跟进了教程并在我的代码中成功嵌入了Python。



Good Morning Sir,

I have referred this article, Embedding Python program in a C/C++ code[^] I have followed up the tutorial and successfully embedded Python in my code.

PyRun_SimpleString("print('Hello World from Embedded Python!!!')");





s用于执行单行Python代码但是如何运行Python中的多行代码。我也试过从文件中运行Python,但是文件也需要在那里运行可执行文件。哭| :((我不想要一个文件和我的可执行文件,我搜索谷歌先生,但我发现没有结果。



我在这里很困惑,我不知道是什么to doConfused |:confused:



谢谢先生您的时间



我有什么试过:



1.试图联系文章的作者,等待 他反应迟钝。



2.尝试了一些谷歌搜索,但我找不到任何相关内容,我仍在等待作者回复。



非常感谢任何帮助



谢谢你的时间先生!



s used to execute a single line of Python code but how do I run a multiple lines of code in Python. I also tried running a Python from a file but the file also needed to be there for running the executable. Cry | :(( I dont want a file along with my executable, I searched in the google sir but I find no results.

I am confused here, I dont know what to doConfused | :confused:

Thank you sir for your time

What I have tried:

1. tried to contact the author of the article, waited for a month and he is unresponsive.

2. Tried some Googling but I don't find anything relevant, and I am still waiting for the author to respond.

Any help is much appreciated

Thank you sir for your time!

推荐答案

好吧,您可以简单地将python文件转换为二进制文件并将其编译/链接到您的程序二进制文件中。在运行时,获取已编译的二进制文本(此时应该是一些const char *)并将其作为字符串提供给Python。



然而,这种方法使您的应用成为可能每次你需要进行任何Python更改时都必须编译...这首先在使用Python时失去了很大的可取性。换句话说......那时为什么要使用Python?
Well, you could simply turn the python file into binary and compile/link it into your program binary. At run time, take the compiled binary text (which should be some const char* at this point) and feed it to Python as a string.

This approach however, makes your application have to compile every time you need any Python changes... which loses a big part of the desirability of using Python in the first place. In another words... at that point why use Python at all?


这篇关于在C ++中嵌入Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆