Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CPython扩展如何能够调用CPython内部定义的函数?_Python_C_Dll_Cpython - Fatal编程技术网

CPython扩展如何能够调用CPython内部定义的函数?

CPython扩展如何能够调用CPython内部定义的函数?,python,c,dll,cpython,Python,C,Dll,Cpython,在中,可以找到以下代码: static PyObject * spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) return NULL; sts = system(command); return PyLong_FromLong(sts); }

在中,可以找到以下代码:

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return PyLong_FromLong(sts);
}
我们可以看到,外部C扩展中的这个函数能够使用(我认为)在主CPython解释器源代码中定义的函数:
PyArg\u ParseTuple

如果我们只是直接构建扩展源文件(aka
gcc-shared myextension.c
,等等),即使包含必要的头
,链接器也会抱怨
对PyArg\u parsetup的引用未定义

那么,如何构建CPython扩展,以允许它们自由引用CPython代码库中的函数呢

扩展是否与实际解释器的源代码一起构建?它们是否与实际解释器的对象文件相链接?另一种方法


请参考与Windows相关的方法。还欢迎提供有关Linux的更多信息。

在Linux中,相关的库调用是,其中“dl”表示动态链接。委员会:

如果可执行文件与标志“
-rdynamic
”(或同义词“
-export dynamic
”)链接,则可执行文件中的全局符号也将用于解析动态加载库中的引用


换句话说,动态链接器在加载时解析动态加载库中的引用。

在我的例子中,在运行主机可执行文件之前,我在编译点失败,因为链接器抱怨
未定义的引用。有什么想法吗?