我正在尝试从C代码中调用Python函数,我遵循了这里的示例。我还有正确的包含文件目录、库目录,并链接了python32.lib(我使用的是Python 32),但错误是Python/C API,如PyString_FromString、PyInt_FromLong、PyInt_AsLong未定义(在调试器中出现错误)。这很奇怪,因为我也在使用其他API,但它们都没问题... 这里有什么问题?
Py_Initialize()、PyImport_Import()、PyModule_GetDict() 都可以正常工作,但 PyString_FromString(...) 不行。
int
main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize();
pName = PyString_FromString(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pDict = PyModule_GetDict(pModule);
/* pDict is a borrowed reference */
Py_Initialize()、PyImport_Import()、PyModule_GetDict() 都可以正常工作,但 PyString_FromString(...) 不行。