简体   繁体   English

嵌入式 python 在树莓派上编译失败

[英]Embedded python fails to compile on Raspberry Pi

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.我按照此处的说明在我的 Raspberry Pi 上安装了 python 3.9.1 https://www.ramoonus.nl/2020/10/06/how-to-install-python-3-9-on-raspberry-pi/并设置它作为默认的 python 解释器。 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我按照此处的说明获得了嵌入式 Python 的编译和链接参数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):我尝试使用以下代码 (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.尝试编译位于https://docs.python.org/3.9/extending/embedding.html#pure-embedding的示例会引发相同的错误。 What could the problem be?可能是什么问题?

Edit: After Expolarity's comment I changed the linker command to:编辑:在 Expolarity 发表评论后,我将 linker 命令更改为:

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 test.o: 在 function main': /home/pi/Downloads/test.c:6: undefined reference to ' /home/pi/Downloads/test.c:11: 对Py_SetProgramName' /home/pi/Downloads/test.c:12: undefined reference to未定义引用Py_SetProgramName' /home/pi/Downloads/test.c:12: undefined reference to Py_Initialize 的未定义引用/home/pi/Downloads/test.c:13:对PyRun_SimpleStringFlags' /home/pi/Downloads/test.c:15: undefined reference to / home/pi/Downloads/test.c:16: 未定义对“PyMem_RawFree”的引用 collect2: error: ld returned 1 exit status

This seems more serious.这似乎更严重。 Any ideas?有任何想法吗?

After tttapa's answer over here it finally worked by adjusting the linker command as so:在 tttapa 在这里回答之后,它终于通过调整 linker 命令来工作:

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.编辑:在 tttapa 之后,Expolarity 也正确回答了。 Thanks a lot everyone!非常感谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM