问题描述:
程序执行至调用函数前都没有错误,但返回的结果为空,即pReturn为空。想请教各位这是什么原因?另外,在释放python对象指针代码处,VS2010编译报错(之前灰掉这块了):
“ 无法解析的外部符号 imp_Py_Dealloc,该符号在函数 main 中被引用
1>TT.obj : error LNK2019:
无法解析的外部符号 __imp__Py_NegativeRefcount,该符号在函数 main 中被引用
1>TT.obj : error LNK2001: 无法解析的外部符号 __imp__Py_RefTotal”
请大神指点!以下是代码块。
python代码
#-*- coding:gb2312 -*-
def hello(a,b) :
return a+b
c++代码
#include<iostream>
#include<Python.h>
using namespace std;
int main()
{
Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
PyObject * pModule = NULL;//声明变量
PyObject * pFunc = NULL;// 声明变量
PyObject *pDict = NULL;
PyObject *pArgs = NULL;
pModule =PyImport_ImportModule("test_call");//这里是要调用的文件名
if ( !pModule )
{
printf("can't find pytest.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
//pFunc= PyObject_GetAttrString(pModule, "hello");//这里是要调用的函数名
pFunc = PyDict_GetItemString(pDict, "hello");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [hello]");
getchar();
return -1;
}
pArgs = Py_BuildValue("i",3);
pArgs = Py_BuildValue("i",4);
PyObject *pReturn = NULL;
pReturn = PyEval_CallObject(pFunc, pArgs);//调用函数
if(!pReturn)
{
cout<<"null.\n";
return -1;
}
int result = -1;
if (pReturn && PyArg_ParseTuple(pReturn,"i", &result))
{
cout << "3+4 = " << result << endl;
}
Py_DECREF(pModule);
Py_DECREF(pFunc);
Py_DECREF(pDict);
Py_DECREF(pArgs);
Py_Finalize();//调用Py_Finalize,这个根Py_Initialize相对应的。
return 0;
}
是不是py文件编码格式问题?找不到对应函数名
#!/usr/bin/python
#coding:utf-8
def hello(a,b):
return a+b
if __name__ == '__main__':
c=hello(a,b)
assert(c == a+b)
去掉python函数中的print语句
你是怎么解决的?我也出现了这样的问题
解决方法
修改两个头文件
1 注释掉object.h第56行
//#define Py_TRACE_REFS
2 pyconfig.h 375行
//# define Py_DEBUG
以上两个宏定义注释掉以后重新编译 问题解决
这篇博客有答案:
https://blog.csdn.net/jacke121/article/details/105819340
解决了吗,我也有这问题