简体   繁体   English

从 C++ 代码运行 python 脚本并在 C++ 中使用 pythons 输出

[英]Running python script from c++ code and use pythons output in c++

I'm have a C++ program that is pretty far developed to do it's own job and now we'd like to add an addition functionality to it and we thought that making said functionality in Python and then calling that python with required inputs from the C++ when needed would be the best way to go as it keeps them separated and allows us to use this python script from elsewhere too.我有一个 C++ 程序,它已经非常发达,可以完成它自己的工作,现在我们想向它添加一个附加功能,我们认为在 Python 中实现上述功能,然后使用来自 C++ 的所需输入调用该 Python需要时将是最好的方法,因为它将它们分开并允许我们也从其他地方使用这个 python 脚本。

As a first step I decided to try to make a test program to see how this would work and seems like it was a good idea because I can't get it to work.作为第一步,我决定尝试制作一个测试程序来看看它是如何工作的,这似乎是一个好主意,因为我无法让它工作。

How do I run separate python from c++?如何从 C++ 中运行单独的 python?

I have tried following this guide and while it seems good it doesn't give any information on what compiler options should I run this with?我已经尝试遵循本指南,虽然看起来不错,但它没有提供有关我应该使用哪些编译器选项运行它的任何信息?

I have two files, cpp.cpp and python.py我有两个文件,cpp.cpp 和 python.py

This is my cpp.cpp file:这是我的 cpp.cpp 文件:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <ncurses.h>
#include <Python.h>

using namespace std;

int main() {

    std::cout << "C++ program started!\n";
    
    char filename[] = "python.py";
    FILE* fp;
    
    Py_Initialize();
    
    fp = _Py_fopen(filename, "r");
    PyRun_SimpleFile(fp, filename);
    
    Py_Finalize();
    
    std::cout << "C++ program is ending!\n";
    return 0;
}

and my python file is just two printf line:我的python文件只是两个printf行:

#print('External Python program running...')
#print('Hello World from Python program')

I then try to compile this, give it all the includes it seems to want and then execute the output file:然后我尝试编译它,给它所有它似乎想要的包含,然后执行输出文件:

g++ -I . -I /home/ahomm/python3.6/Include -I /home/ahomm/python3.6/release cpp.cpp && ./a.out

This is the output I get:这是我得到的输出:

/tmp/cccQsh1p.o: In function `main':
cpp.cpp:(.text+0x3f): undefined reference to `Py_Initialize'
cpp.cpp:(.text+0x52): undefined reference to `_Py_fopen'
cpp.cpp:(.text+0x70): undefined reference to `PyRun_SimpleFileExFlags'
cpp.cpp:(.text+0x75): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status

What am I missing?我错过了什么? is just something just a little or completely wrong?只是有一点点错误或完全错误吗? cpp and py files and located in the same directory. cpp 和 py 文件并位于同一目录中。

And how do I then read the output of python in C++?然后我如何在 C++ 中读取 python 的输出? Haven't even got to that yet...还没到那一步...

You have to link your code with libpython3.xa/python3.x.lib (x - version of python you use).您必须将您的代码与 libpython3.xa/python3.x.lib(x - 您使用的 python 版本)链接起来。 Which file to link: *.a or *.lib depends of your OS.链接哪个文件:*.a 或 *.lib 取决于您的操作系统。 The files are available with python distribution.这些文件在 python 发行版中可用。

Here is a code with cmake that works for me:这是一个对我有用的带有 cmake 的代码:

cmake_minimum_required(VERSION 2.8.9)
project (embpy)
add_executable(embpy embpy.cpp)
target_include_directories(embpy PRIVATE /path-to-python/Python38/include/python3.8)
target_link_libraries(embpy /path-to-python/Python38/lib/libpython3.8.a)

the embpy.cpp is the same as yours embpy.cpp 和你的一样

Figured it out myself then, the problem was incomplete compiler arguments.然后自己想通了,问题是不完整的编译器参数。

This is what I got it to works with:这就是我让它使用的:

g++ -fPIC $(python3.6-config --cflags) cpp.cpp $(python3.6-config --ldflags)

the key missing parts were $(python3.6-config --cflags) before and $(python3.6-config --ldflags) after the file that was to be compiled.关键缺失部分为$(python3.6-config --cflags)之前和$(python3.6-config --ldflags)这是被编译后的文件。 The first one gives g++ the compile options and the latter gives the flags for linking.第一个给 g++ 编译选项,后者给链接标志。

Found the solution from python docs , part 1.6.python docs 1.6 部分找到解决方案。

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

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