簡體   English   中英

嵌入式 python 在樹莓派上編譯失敗

[英]Embedded python fails to compile on Raspberry Pi

我按照此處的說明在我的 Raspberry Pi 上安裝了 python 3.9.1 https://www.ramoonus.nl/2020/10/06/how-to-install-python-3-9-on-raspberry-pi/並設置它作為默認的 python 解釋器。 我按照此處的說明獲得了嵌入式 Python 的編譯和鏈接參數https://docs.python.org/3.9/extending/embedding.html#compiling-and-linking-under-unix-like-systems

我嘗試使用以下代碼 (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.o

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

並得到

/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 發表評論后,我將 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

這似乎有效但給我帶來了很多其他錯誤:

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

這似乎更嚴重。 有任何想法嗎?

在 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

編輯:在 tttapa 之后,Expolarity 也正確回答了。 非常感謝大家!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM