Embedded python fails to compile on Raspberry Pi - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Embedded python fails to compile on Raspberry Pi (/thread-31605.html) |
Embedded python fails to compile on Raspberry Pi - tryfon - Dec-22-2020 Hi all! I installed python 3.9.1 on my Raspberry Pi following the instructions here and set it as the default python interpreter. I got my compiling and linking parameters for embedded Python following the instructions here I tried a simple test with the following code (test.c) : int main(int argc, char *argv[]) { wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); exit(1); } Py_SetProgramName(program); /* optional but recommended */ Py_Initialize(); PyRun_SimpleString("from time import time,ctime\n" "print('Today is', ctime(time()))\n"); Py_Finalize(); PyMem_RawFree(program); return 0; }and then gcc -I/usr/local/opt/python-3.9.1/include/python3.9 -I/usr/local/opt/python-3.9.1/include/python3.9 -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -c test.c -o test.o and gcc -L/usr/local/opt/python-3.9.1/lib/python3.9/config-3.9-arm-linux-gnueabihf -L/usr/local/opt/python-3.9.1/lib -lcrypt -lpthread -ldl -lutil -lm -o test.o and got Trying to compile the example over here throws the same error. What could the problem be?
RE: Embedded python fails to compile on Raspberry Pi - tryfon - Dec-22-2020 I made some progress after changing the linker command to: gcc test.o -L/usr/local/opt/python-3.9.1/lib/python3.9/config-3.9-arm-linux-gnueabihf -L/usr/local/opt/python-3.9.1/lib -lcrypt -lpthread -ldl -lutil -lm -o test but I got a bunch of other errors: This seems more serious. Any ideas?
RE: Embedded python fails to compile on Raspberry Pi - tryfon - Dec-22-2020 OK, problem solved. It seems that the behavior of the python3-config script was changed in Python 3.8: https://docs.python.org/3/whatsnew/3.8.html#debug-build-uses-the-same-abi-as-release-build To embed Python into an application, a new --embed option must be passed to python3-config --libs --embed to get -lpython3.8 (link the application to libpython). To support both 3.8 and older, try python3-config --libs --embed first and fallback to python3-config --libs (without --embed) if the previous command fails. So I changed the linker command to gcc test.o -L/usr/local/opt/python-3.9.1/lib/python3.9/config-3.9-arm-linux-gnueabihf -L/usr/local/opt/python-3.9.1/lib -lcrypt -lpthread -ldl -lutil -lm -lpython3.9 -o test and it finally worked! Thanks a lot everyone! |