Cython 在 C 程序中嵌入 Python - 无法链接到 embed.o

问题描述 投票:0回答:0

嗨,我是将源代码从 python 嵌入到 C 中的新手。 我有一个创建名为“介绍”的 cython 模块的基本程序。 “介绍”模块包含一个称为“介绍”的功能。

cdef char* introduction(char* name, char* lastName):
    return "Hello, {} {}!".format(name, lastName).encode("utf-8")

然后我们在这个程序中调用它:

/**
File            :   embed.c
Project         :   None
Programmer      :   Braiden Gole
First version   :   2023-04-22
Description     :   This is my first embeded python program.

NOTE: Where Python is located.
NOTE: C:\Users\<my_username>\AppData\Local\Programs\Python\Python311\include
NOTE: "C:\Users\<my_username>\AppData\Local\Programs\Python\Python311\libs\python311.lib"
NOTE: "C:\Users\<my_username>\AppData\Local\Programs\Python\Python311\python311.dll"

COMPILE:
gcc -c embed.c -o embed.o -IC:/Users/<my_username>/AppData/Local/Programs/Python/Python311/include
LINK:
gcc -o embed embed.o -IC:/Users/<my_username>/AppData/Local/Programs/Python/Python311/include -LC:/Users/<my_username>/AppData/Local/Programs/Python/Python311/libs -lpython311
*/
#include <stdio.h>
#include <Python.h>

int main(int argc, char* argv[])
{   
    // Initialize the python interpreter.
    Py_Initialize();

    // Import the CPython module.
    PyObject* pModule = PyImport_ImportModule("introduction");
    if (!pModule)
    {
        printf("%s", "Failed to imprt python module.");
        return 1;
    }

    PyObject* pFunction = PyObject_GetAttrString(pModule, "introduction");

    if (!pFunction || !PyCallable_Check(pFunction))
    {
        printf("%s", "Failed to get python function.");
        return 1;
    }

    // Create a tuple and set the arguments of the function.
    PyObject* pArguments = PyTuple_New(2);
    PyTuple_SetItem(pArguments, 0, PyUnicode_FromString("Braiden"));
    PyTuple_SetItem(pArguments, 1, PyUnicode_FromString("Gole"));

    PyObject* pResult = PyObject_CallObject(pFunction, pArguments);
    Py_DECREF(pArguments);

    if (!pResult)
    {
        printf("%s", "Failed to call python function.");
        return 1;
    }

    char* introduction = PyBytes_AsString(pResult);
    printf("%s", introduction);
    Py_DECREF(pResult);

    // Clean up resources.
    Py_DECREF(pFunction);
    Py_DECREF(pModule);
    Py_Finalize();
    return 0;
}

注意代码注释中显示的路径以及这些路径结尾的文件。这些是我从系统中获得的路径值,所以我知道这些目录和文件存在。

我可以编译

embed.c
但我不能链接目标文件
embed.o
.

你能帮我解决这个问题吗?

注意事项:

  • Python 安装到 PATH
  • 已安装pythons调试符号
  • 已安装 pythons 调试二进制文件。
  • 我刚升级到 Python 3.11.3
  • 我尝试链接到我的旧 Python 安装 Python 3.10,但仍然没有链接。

这是我尝试链接文件时得到的结果:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x23): undefined reference to `_imp___Py_Dealloc'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x3b): undefined reference to `_imp__Py_Initialize'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x49): undefined reference to `_imp__PyImport_ImportModule'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x88): undefined reference to `_imp__PyObject_GetAttrString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xa1): undefined reference to `_imp__PyCallable_Check'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xd1): undefined reference to `_imp__PyTuple_New'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xe3): undefined reference to `_imp__PyUnicode_FromString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xfd): undefined reference to `_imp__PyTuple_SetItem'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x10b): undefined reference to `_imp__PyUnicode_FromString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x125): undefined reference to `_imp__PyTuple_SetItem'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x13b): undefined reference to `_imp__PyObject_CallObject'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x17b): undefined reference to `_imp__PyBytes_AsString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x1be): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status

谢谢你的时间。

python-3.x cython
© www.soinside.com 2019 - 2024. All rights reserved.