我在 Visual Studio 中遇到链接错误,函数 _main 中引用了未解析的外部符号 __ipm_Py_Finalize 等。 我已在链接器附加库目录中添加了 python 的 Library 文件夹,并在 C/C++ 附加包含目录中添加了 include 文件夹,我不知道我做错了什么,但这是我的 main.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!");
}
这可能是一些 IDE 错误还是我做错了。 谢谢。
除此之外: 我只在我的 VS IDE 的链接步骤中遇到错误,并且编译成功,没有任何错误。
好吧,在本节中,我没有包含 python3.lib,对此的简单修复是在链接器中包含 python3.lib。