2
votes

I installed python 3.9.1 on my Raspberry Pi following the instructions here https://www.ramoonus.nl/2020/10/06/how-to-install-python-3-9-on-raspberry-pi/ and set it as the default python interpreter. I got my compiling and linking parameters for embedded Python following the instructions here https://docs.python.org/3.9/extending/embedding.html#compiling-and-linking-under-unix-like-systems

I tried a simple test with the following code (test.c) :

#include <Python.h>

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

/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 at https://docs.python.org/3.9/extending/embedding.html#pure-embedding throws the same error. What could the problem be?

Edit: After Expolarity's comment 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 -o test

which seems to have worked but threw me a bunch of other errors:

test.o: In function main': /home/pi/Downloads/test.c:6: undefined reference to Py_DecodeLocale' /home/pi/Downloads/test.c:11: undefined reference to Py_SetProgramName' /home/pi/Downloads/test.c:12: undefined reference to Py_Initialize' /home/pi/Downloads/test.c:13: undefined reference to PyRun_SimpleStringFlags' /home/pi/Downloads/test.c:15: undefined reference to Py_Finalize' /home/pi/Downloads/test.c:16: undefined reference to `PyMem_RawFree' collect2: error: ld returned 1 exit status

This seems more serious. Any ideas?

1
The line gcc /usr/bin/g++ -L/usr/local/opt/python-3.9.1/... doesn't contain the object file test.o (which contains your main function) and would try to compile or link /usr/bin/g++ instead. Don't you get an error message about /usr/bin/g++? - Bodo
I believe typing just test as I have done instead of test.o is the same. I tried test.o and still throws the same error. - tryfon
If you tried other commands, please edit your question and copy&paste the exact command and the output instead of vaguely describing what you did. To be more clear about my previous comment: In the linker command, remove /usr/bin/g++ and add test.o instead. - Bodo
you're attempting to run an unlinked ELF object file, you need to link it in order for it to be an ELF executable. to be exact, this command should work: 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 - Expolarity
Alright, that's progress! It seems you forgot to link towards the python library, try 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 -lpython3.9 -lcrypt -lpthread -ldl -lutil -lm -o test, the uppercase -L option specifies directories to look for libraries (which are then specified using the lowercase -l) - Expolarity

1 Answers

1
votes

After tttapa's answer over here it finally worked by adjusting the linker command as so:

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

Edit: Expolarity also answered correctly just after tttapa. Thanks a lot everyone!