如何解决 C Python API 中的链接错误

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

我在 Visual Studio C 项目中遇到链接错误。这些与 C-Python API 的外部函数在链接阶段未解析有关。其中之一是:

unresolved external symbol __ipm_Py_Finalize referenced in function _main
我已在链接器附加库目录中添加了Python的库文件夹,并在C/C++附加包含目录中添加了包含文件夹。这是我的 C 代码:

#include <stdio.h>
#include <stdbool.h>

#include <Python.h>

int main() {
    Py_Initialze();
    printf("Initialized python interpreter!");
    PyObject* module = PyImport_ImportModule("CPBridger");
    if (module == NULL) {
        printf("Can't find module!");
        return -1;
    }

    PyObject* func = PyObject_GetAttrString(module, "helloWorld");
    PyObject* args = PyTuple_Pack(0);
    PyObject_CallObject(func, args);

    Py_XDECREF(func);
    Py_XDECREF(args);

    func = PyObject_GetAttrString(module, "sayHelloTo");
    args = PyTuple_Pack(1, PyString_FromString("LakshyaK2011"));
    PyObject_CallObject(func, args);

    Py_XDECREF(func);
    Py_XDECREF(args);
    Py_XDECREF(module);

    Py_Finalize();
    printf("Finalized python interpreter!");
}

我应该在VS中配置什么来让它解析外部引用?

python c python-c-api
1个回答
0
投票

好吧,在本节中,我没有包含 python3.lib,对此的简单修复是在链接器中包含 python3.lib。

© www.soinside.com 2019 - 2024. All rights reserved.