Python Forum
Embedded python fails to compile on Raspberry Pi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedded python fails to compile on Raspberry Pi
#1
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
Error:
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/crt1.o: In function '_start': /build/glibc-P1SmLh/glibc-2.19/csu/../ports/sysdeps/arm/start.S:119: undefined reference to 'main' collect2: error: ld returned 1 exit status
Trying to compile the example over here throws the same error. What could the problem be?
Reply
#2
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:

Error:
test.o: In function `main': /home/pi/Downloads/test.c:16: undefined reference to `Py_Initialize' /home/pi/Downloads/test.c:17: undefined reference to `PyUnicode_DecodeFSDefault' /home/pi/Downloads/test.c:20: undefined reference to `PyImport_Import' /home/pi/Downloads/test.c:24: undefined reference to `PyObject_GetAttrString' /home/pi/Downloads/test.c:27: undefined reference to `PyCallable_Check' /home/pi/Downloads/test.c:55: undefined reference to `PyErr_Occurred' /home/pi/Downloads/test.c:56: undefined reference to `PyErr_Print' test.o: In function `_Py_DECREF': /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' test.o: In function `main': /home/pi/Downloads/test.c:67: undefined reference to `Py_FinalizeEx' test.o: In function `_Py_DECREF': /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' test.o: In function `main': /home/pi/Downloads/test.c:55: undefined reference to `PyErr_Occurred' /home/pi/Downloads/test.c:56: undefined reference to `PyErr_Print' /home/pi/Downloads/test.c:28: undefined reference to `PyTuple_New' /home/pi/Downloads/test.c:38: undefined reference to `PyTuple_SetItem' /home/pi/Downloads/test.c:30: undefined reference to `PyLong_FromLong' test.o: In function `_Py_DECREF': /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' test.o: In function `main': /home/pi/Downloads/test.c:40: undefined reference to `PyObject_CallObject' /home/pi/Downloads/test.c:43: undefined reference to `PyLong_AsLong' test.o: In function `_Py_DECREF': /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' test.o: In function `main': /home/pi/Downloads/test.c:63: undefined reference to `PyErr_Print' test.o: In function `_Py_DECREF': /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' test.o: In function `main': /home/pi/Downloads/test.c:49: undefined reference to `PyErr_Print' test.o: In function `_Py_DECREF': /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' /usr/local/opt/python-3.9.1/include/python3.9/object.h:430: undefined reference to `_Py_Dealloc' collect2: error: ld returned 1 exit status
This seems more serious. Any ideas?
Reply
#3
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.h...ease-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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Compile error ajitnayak1987 4 450 Jun-03-2021, 12:11 PM
Last Post: ibreeden
  python 3 raspberry pi 4 dual control motor programming problem yome 0 368 Mar-21-2021, 05:17 PM
Last Post: yome
  Can Embedded Python run any shared library on Android ? sprotz 0 502 Nov-08-2020, 12:21 PM
Last Post: sprotz
  pip3 install opencv-python fails on 'skbuild' Pedroski55 2 1,954 Sep-15-2020, 11:33 AM
Last Post: snippsat
  Uploading files to NAS from Raspberry PI using python zazas321 0 577 Sep-07-2020, 09:02 AM
Last Post: zazas321
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 727 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Python/C API project - compile to exe MikeTrusky 0 582 Jul-22-2020, 03:55 PM
Last Post: MikeTrusky
  memory leak on embedded python in c++ asdf3721 3 983 Jul-16-2020, 06:33 AM
Last Post: Gribouillis
  Python 3.6.10 fails when running a localized script. Mikee 2 642 Jul-09-2020, 10:25 PM
Last Post: Mikee
  Compile to Windows exe. SpongeB0B 6 1,298 Jul-06-2020, 08:44 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020