嵌入段错误的Python解释器的最小示例

我从这里复制的例子: https : //docs.python.org/3.5/extending/embedding.html

#include  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 -fno-diagnostics-color -Wall -Wno-unused-function -fPIC -ggdb \ -I. -I/usr/include/python3.5m -c test_embed.c -o test_embed.o gcc -fno-diagnostics-color -Wall -Wno-unused-function -fPIC -ggdb \ -I. -I/usr/include/python3.5m -shared -lpython3.5m ./test_embed.o \ -o test_embed 

运行时段错误并将堆栈破坏到无法弄清楚发生了什么。 我是否需要任何特定设置来编译它?


当我使用/usr/bin/python3.5-config --cflags/usr/bin/python3.5-config --ldconfig给出的编译器选项时,该示例将无法构建,因为它无法在共享中找到符号宾语。


根据要求,这里是编译和链接命令以及错误输出:

 $ gcc $(python3.5-config --cflags) -c test_embed.c -o test_embed.o $ gcc $(python3.5-config --ldflags) ./test_embed.o -o test_embed ./test_embed.o: In function `main': redacted/test_embed.c:6: undefined reference to `Py_DecodeLocale' redacted/test_embed.c:11: undefined reference to `Py_SetProgramName' redacted/test_embed.c:12: undefined reference to `Py_Initialize' redacted/test_embed.c:13: undefined reference to `PyRun_SimpleStringFlags' redacted/test_embed.c:15: undefined reference to `Py_Finalize' redacted/test_embed.c:16: undefined reference to `PyMem_RawFree' collect2: error: ld returned 1 exit status $ python3.5-config --ldflags -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions 

您的test_embed二进制段错误,因为不打算运行使用--shared (共享库)构建的二进制文件。

诊断导致错误使用的构建问题 – 在链接器命令行中python3.5-config可能需要其他详细信息,例如您使用的特定gcc命令以及运行它们的错误消息,或者您的python3.5-config输出系统,也许两者。 如果我使用以下命令编译您的测试程序,它不会发生段错误(它打印预期的输出)。

 gcc $(python3.5-config --cflags) -c test_embed.c -o test_embed.o gcc $(python3.5-config --ldflags) ./test_embed.o -o test_embed 

在我的系统上, python3.5-config --ldflags的输出不包括python3.5-config --ldflags

构建Building C和C ++ Extensions的文档包括其distutils部分中的示例编译命令。 其中的示例链接命令包括--shared它生成的.so文件,但该.so文件旨在导入到python解释器中,而不是作为命令调用。