从 c++ 调用 python 分发计划
我想从我的 C++ 程序中调用 python 脚本文件。
我不确定我将分发给的人是否安装了 python。
I would like to call python script files from my c++ program.
I am not sure that the people I will distribute to will have python installed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
通过套接字使用进程间通信 (IPC) 可能是一种可能的解决方案。 使用本地网络套接字在两者之间侦听/传输命令。
Using Inter Process Communication (IPC) over socket can be a possible solution. Use a local network socket to listen/trasfer commands between both.
Boost 可能是最好的选择,但是如果您想要更独立的东西,并且如果这是用于 Windows 的(这似乎可行,因为他们是最不可能安装 Python 的人),那么您可以使用 py2exe 创建具有适合 COM 对象入口点的 DLL。 然后您可以通过 COM 与该库交互。 (显然,这作为跨平台解决方案根本没有用)。
Boost is probably the best choice, however if you're wanting something that's more standalone, and if this is for use with Windows (which seems feasible given that they are the people least likely to have Python installed), then you can use py2exe to create a DLL with entry points suitable for COM objects. You can then interface with the library via COM. (Obviously this is not at all useful as a cross-platform solution).
在 C++ 应用程序中嵌入 Python 解释器将允许您使用应用程序运行 Python 脚本来运行 Python 程序。 它还将使这些脚本能够更轻松地调用应用程序中的 C++ 函数。 如果这是您想要的,那么前面提到的 Boost 库可能就是您想要的,以便更轻松地创建链接。 过去,我曾使用 SWIG 生成 C++ 代码的 Python 接口。 从你的问题中并不清楚你是否希望Python脚本调用你的C++程序,或者你是否只希望C++调用Python。
许多 Python 函数使用的模块并未内置于 Python 解释器中。 如果您的 Python 脚本调用这些函数,那么您需要让用户安装 Python 或在您的应用程序中包含 python 运行时文件。 这取决于您在 Python 脚本中导入的模块。
Embeding the Python interpreter inside your C++ app will let you run Python programs using your application run Python scripts. It will also make it easier possible for those scripts to call C++ functions in your application. If this is what you want then the Boost library mentioned previously may be what you want to make it easier to create the link. In the past I have used SWIG to generate Python interfaces to C++ code. It was not clear from your question whether you wanted the Python scripts to call your C++ program or whether you just wanted the C++ to call Python.
Many of the Python functions use modules which are not built into the Python interpreter. If your Python scripts call these functions then you will either need to have your users install Python or include the python runtime files with your application. It will depend on what modules you import in you Python scripts.
使用系统调用从 C++ 运行 python 脚本
Use system call to run a python script from C++
有趣的是,目前还没有人提到 pybind11。 从他们的文档中:
具体来说,调用 Python 函数(称为嵌入)就这么简单(取自 文档):
Interestingly, nobody has mentioned pybind11, yet. From their documentation:
Concretely, calling into a Python function (called embedding) is as simple as this (taken from the documentation):
Boost 有一个 python 接口库可以帮助你。
Boost.Python
Boost has a python interface library which could help you.
Boost.Python
这意味着您希望将 Python 嵌入到您的 C++ 应用程序中。 正如在另一个应用程序中嵌入Python中提到的:
我建议您首先阅读在另一个应用程序中嵌入Python。 然后参考以下示例
在 C/C++ 中嵌入 Python:第一部分
在 C/C++ 中嵌入 Python:第二部分
在多线程 C/C++ 应用程序中嵌入 Python
如果您喜欢Boost.Python,您可以访问以下链接:
This means that you want to embed Python in your C++ application. As mentioned in Embedding Python in Another Application:
I suggest that you first go through Embedding Python in Another Application. Then refer the following examples
Embedding Python in C/C++: Part I
Embedding Python in C/C++: Part II
Embedding Python in Multi-Threaded C/C++ Applications
If you like Boost.Python, you may visit the following links: