Ledi
Ledi

Reputation: 51

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.

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++?

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

This is my cpp.cpp file:

#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:

#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.

And how do I then read the output of python in C++? Haven't even got to that yet...

Upvotes: 2

Views: 1696

Answers (2)

Stanislav Melnikov
Stanislav Melnikov

Reputation: 328

You have to link your code with libpython3.x.a/python3.x.lib (x - version of python you use). Which file to link: *.a or *.lib depends of your OS. The files are available with python distribution.

Here is a code with cmake that works for me:

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

Upvotes: 1

Ledi
Ledi

Reputation: 51

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. The first one gives g++ the compile options and the latter gives the flags for linking.

Found the solution from python docs, part 1.6.

Upvotes: 0

Related Questions