0

I embedded a python interpreter in C++ code. It works fine for the high-level way with:

PyRun_SimpleFileEx(fd, "/file_path/some_script.py", 1)

But I need to pass some arguments to the python code so I tried to go forward with some code from the python documentation about embedding. However, XCode complains about some functions that are not defined. My guess is that my

#include <Python/Python.h>

Does not add the right python version and is pointing possibly to the system version (2.7) which doesn't have the headers with the right functions.

My linker Flags are:

-lpython3.7m -ldl -framework CoreFoundation

I added the Python include folder from the framework to the header search paths:

../../Support/Lib/Python/include

and the Python Lib folder from the framework to the library search paths:

../../Support/Lib/Python/lib

The support folder is like indicated to folders up from the solution.

The functions that XCode cannot find are PyUnicode_DecodeFSDefault() and Py_FinalizeEx().

The first one is defined in unicodeobject.h that gets included by Python.h its right where it has to be in the include folder as I described above.

I'm not sure what I'm doing wrong, so maybe someone can point me in the right direction. Thanks in advance!

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • You can use Python 2.7 headers, period. You have to have Python 3.7 headers coming first in the list of your include paths, otherwise, you will get random success, or failures. – Matthieu Brucher Apr 16 '19 at 09:33
  • Using 2.7 headers worked to solve the errors but the python script i want to execute is python 3. So i need to get the right link for python3. Putting the include on top didn't solve the issue it is still linking python2. Any other ideas? – el_flamenco Apr 16 '19 at 10:32
  • So #include definetly includes the default python 2.7 from the system. Any idea how to change this? – el_flamenco Apr 16 '19 at 11:10
  • No, it's not just the link, you also need to get the Python 3 headers. They are not compatible one with the other! (my comment remove the can**'t**). You **CANNOT** use Python 2 headers and link against Python 3. – Matthieu Brucher Apr 16 '19 at 13:35
  • Yes your right and i did change the links and the header files that were linked to. In the end the problem was that i didn't put the correct include statement so when i took #include it allways included the system default Python2. So the problem wasn't really the links. But my link path also didn't go till the last folder that had the header in it. Sorry for the confusion i'm quite new to this topic. Thanks for your help! – el_flamenco Apr 16 '19 at 14:31
  • Because "Python/Python.h" is the usual proper pattern. The way Python 3 is installed is incorrect, as it's not standard. – Matthieu Brucher Apr 16 '19 at 14:35
  • How should python3 be installed correctly to work with #include ? I just copied the include and lib folders of my pyenv Python Frameworks to my Support Folder and used those. – el_flamenco Apr 17 '19 at 05:56
  • Try installing Python 3 instead. – Matthieu Brucher Apr 17 '19 at 07:16

1 Answers1

0

Ok like allways devil is in the details... I figured out i only have to pass the full path directly until the top directory of the header files i need. Subdirectories are not searched. So my header search path is:

../../Support/Lib/Python/include/python3.7m

and my library search path:

../../Support/Lib/Python/lib/python3.7 and then just include

To include i finally just did:

#include <Python.h>