1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    `-L` tells the linker the folder where the libraries are but does not link to anything. Use `-l` to link to individual libraries. – drescherjm Jan 02 '22 at 16:03
  • I dont kown if I follow you: if you mean replacing -L ... by: ```-l C:\Users\...\Python39\libs\python39.lib``` It says it connot find it. – Daniel Casasampera Jan 02 '22 at 16:16
  • Use both `-L` to specify the folder and `-l` to specify the library. – drescherjm Jan 02 '22 at 16:17
  • Here is a question that shows you how to use an external library with your compiler: [https://stackoverflow.com/questions/6016815/how-to-include-needed-c-library-using-gcc](https://stackoverflow.com/questions/6016815/how-to-include-needed-c-library-using-gcc) – drescherjm Jan 02 '22 at 16:18
  • With this still gives me the same error as the one from the question: ```g++ -o run_py.exe -l python39 -L C:\Users\...\Python39\libs -I C:\Users\...\Python39\include run_py.cpp``` – Daniel Casasampera Jan 02 '22 at 16:24
  • 2
    I think you don't want a space between -l and python39. Also you may be mixing 32 and 64 bit. And possibly using the wrong python binaries for mingw. For the latter I recommend switching to use msys2 to provide mingw and use the package manager in msys2 to download and install the python libraries. [https://www.msys2.org/](https://www.msys2.org/) 64 bit: [https://packages.msys2.org/package/mingw-w64-x86_64-python?repo=mingw64](https://packages.msys2.org/package/mingw-w64-x86_64-python?repo=mingw64) – drescherjm Jan 02 '22 at 16:29
  • 32 bit python: [https://packages.msys2.org/package/mingw-w64-i686-python?repo=mingw32](https://packages.msys2.org/package/mingw-w64-i686-python?repo=mingw32) – drescherjm Jan 02 '22 at 16:33
  • @drescherjm, I have msys2 installed and the 64bit package. What should I do now? – Daniel Casasampera Jan 02 '22 at 18:23
  • Use the g++ from C:\msys64\mingw64\bin possibly from the MinGW64 shell: `"C:\msys64\mingw64.exe"` after installing python in msys2 the library will be in the path. – drescherjm Jan 02 '22 at 18:38
  • @drescherjm from the msys64 terminal im writing this command: ```g++ -o run_py.exe -I C:/Users/.../Python39/include -L C:/msys64/mingw64/bin -lpython3.9 run_py.cpp``` Now it lets me compile it but it gives me this error when executing: libpython3.9.dll not found – Daniel Casasampera Jan 02 '22 at 19:14
  • The include path would be `/usr/include/python3.9` and you need `-lpython3.9` – drescherjm Jan 02 '22 at 19:15
  • 1
    ***gives me this error when executing: libpython3.9.dll not found*** How are you executing? If its not from the msys64 terminal it won't be in the path and you will have to look at this Microsoft document on how the OS finds dlls: [https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#standard-search-order-for-desktop-applications](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#standard-search-order-for-desktop-applications) – drescherjm Jan 02 '22 at 19:16
  • Thanks alot @drescherjm, now im running: ```g++ -o run_py.exe -I C:/msys64/mingw64/include/Python3.9 -L C:/msys64/mingw64/bin -lpython3.9 run_py.cpp``` and it compiles it with no errors. I was executing it by double clicking and it gives me the libpython3.9.dll not found. Is this an error that can be fixed by creating the exe file differently? – Daniel Casasampera Jan 02 '22 at 19:49
  • 1
    It can be fixed by copying the required dlls into the same folder as the executable or setting the windows PATH environment variable or several other methods described in the last link I gave you. – drescherjm Jan 02 '22 at 19:52
  • @drescherjm I have copied the python39.dll file of my python folder and renamed to libpython3.9.dll in the folder of the exe file and somehow it works. Is this a safe/valid solution?. At least now it works though. Thanks Alot! – Daniel Casasampera Jan 02 '22 at 20:18

0 Answers0