I would like to be able to execute Python code from a C++ app.
I have zero knowledge on C++, and I only know Python. I am trying to follow the tutorial Embedding Python program in a C/C++ code, but because I don’t know C++, I find it hard to follow.
I am using Windows 10. I have managed to install MinGW as my compiler.
This is the C++ code called run_py.cpp:
#include <stdio.h>
#include <conio.h>
#include <Python.h>
int main()
{
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("print('Hello, World from Embedded Python!!!')");
Py_Finalize();
printf("\nPress any key to exit...\n");
if(!_getch())
_getch();
return 0;
}
I have already installed the Python development tools, so I have Python.h already. I am trying to compile my .cpp file using this command from the Windows terminal:
g++ -I C:\Users\...\Python39\include run_py.cpp -L C:\Users\...\Python39\libs -o run_py.exe
But it gives me this error:
C:\Users\...\AppData\Local\Temp\cceaTqLQ.o:run_py.cpp:(.text+0xf): undefined reference to `_imp__Py_Initialize'
C:\Users\...\AppData\Local\Temp\cceaTqLQ.o:run_py.cpp:(.text+0x25): undefined reference to `_imp__PyRun_SimpleStringFlags'
C:\Users\...\AppData\Local\Temp\cceaTqLQ.o:run_py.cpp:(.text+0x2c): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
How can I fix this?