gpt4 book ai didi

python - 嵌入式python无法在Raspberry Pi上编译

转载 作者:行者123 更新时间:2023-12-03 10:00:08 26 4
gpt4 key购买 nike

我按照https://www.ramoonus.nl/2020/10/06/how-to-install-python-3-9-on-raspberry-pi/的说明在Raspberry Pi上安装了python 3.9.1,并将其设置为默认的python解释器。我按照https://docs.python.org/3.9/extending/embedding.html#compiling-and-linking-under-unix-like-systems的说明获得了嵌入式Python的编译和链接参数
我用以下代码(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;
}
进而 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.ogcc -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并得到 /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尝试在 https://docs.python.org/3.9/extending/embedding.html#pure-embedding上编译示例会引发相同的错误。可能是什么问题?
编辑:
在Expolarity发表评论之后,我将链接器命令更改为: 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这似乎已经奏效,但给我带来了许多其他错误:
test.o:在函数 main': /home/pi/Downloads/test.c:6: undefined reference to Py_DecodeLocale中
/home/pi/Downloads/test.c:11:对 Py_SetProgramName' /home/pi/Downloads/test.c:12: undefined reference to Py_Initialize的 undefined reference
/home/pi/Downloads/test.c:13:对 PyRun_SimpleStringFlags' /home/pi/Downloads/test.c:15: undefined reference to Py_Finalize的 undefined reference
/home/pi/Downloads/test.c:16:对“PyMem_RawFree”的 undefined reference
collect2:错误:ld返回1退出状态
这似乎更为严重。有任何想法吗?

最佳答案

经过tttapa对here的回答后,它终于通过调整链接器命令而工作了: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编辑:极地刺激也可以在tttapa之后正确回答。非常感谢大家!

关于python - 嵌入式python无法在Raspberry Pi上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65407558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com