嵌入segfaults的Python解释器的最小示例 [英] Minimal example of Python interpreter embedding segfaults

查看:97
本文介绍了嵌入segfaults的Python解释器的最小示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处复制的示例: https://docs.python.org/ 3.5 / extending / embedding.html

The example I copied from here: https://docs.python.org/3.5/extending/embedding.html

#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;
}

如果像这样编译和链接:

If compiled and linked like this:

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

在运行时出现段错误,并破坏堆栈以至于无法弄清楚发生了什么。我需要任何特定的设置来进行编译吗?

segfaults when run and destroys the stack to the point it's impossible to figure out what happened. Do I need any specific settings to compile this?

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

When I use compiler options as given by /usr/bin/python3.5-config --cflags and /usr/bin/python3.5-config --ldconfig, the example won't build because it cannot find the symbols in shared object.

每个请求都是编译和链接命令,错误输出:

Per requested, here's the compile and link commands and the error output:

$ 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 (共享库)构建的二进制文件。

Your test_embed binary segfaults because a binary built with --shared (a shared library) is not intended to be run.

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

Diagnosing the build problem which led you to erroneously use --shared in the linker command line might require additional detail, such as the specific gcc commands you used and the error messages from running them, or the output of python3.5-config on your system, perhaps both. If I compile your test program with the following commands it does not segfault (it prints the expected output).

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 的输出不包括-共享

On my system the output of python3.5-config --ldflags does not include --shared.

用于构建构建C和C ++扩展在其 distutils 部分。此处的示例链接命令包括它生成的 .so 文件的-共享,但该 .so 文件旨在导入到python解释器中,而不是作为命令调用。

The documentation for building Building C and C++ Extensions includes sample compilation commands in its distutils section. The sample link command there includes --shared for the .so file it generates, but that .so file is intended to be imported into a python interpreter, not invoked as a command.

这篇关于嵌入segfaults的Python解释器的最小示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆