Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用C+调用sympy+;嵌入 我试图使用Calm和C++程序,并使用下面的嵌入代码来完成。p> int sym_ex(const char * input_expression, const char * output_expression){ PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; const char * file = "sympy"; const char * function = "simplify"; Py_Initialize(); //PyRun_SimpleString("from sympy import *\n"); pName = PyString_FromString(file); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, function); /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New(1); pValue = PyString_FromString(input_expression); printf("the string passed %s\n", input_expression); if (!pValue) { Py_DECREF(pArgs); Py_DECREF(pModule); fprintf(stderr, "Cannot convert argument\n"); return 1; } /* pValue reference stolen here: */ PyTuple_SetItem(pArgs, 0, pValue); pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { printf("Result of call: %s\n", PyString_AsString(pValue)); Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr, "Call failed\n"); return 1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", function); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", file); return 1; } Py_Finalize(); return 0; }_Python_C++_Sympy_Python Embedding - Fatal编程技术网

Python 使用C+调用sympy+;嵌入 我试图使用Calm和C++程序,并使用下面的嵌入代码来完成。p> int sym_ex(const char * input_expression, const char * output_expression){ PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; const char * file = "sympy"; const char * function = "simplify"; Py_Initialize(); //PyRun_SimpleString("from sympy import *\n"); pName = PyString_FromString(file); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, function); /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New(1); pValue = PyString_FromString(input_expression); printf("the string passed %s\n", input_expression); if (!pValue) { Py_DECREF(pArgs); Py_DECREF(pModule); fprintf(stderr, "Cannot convert argument\n"); return 1; } /* pValue reference stolen here: */ PyTuple_SetItem(pArgs, 0, pValue); pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { printf("Result of call: %s\n", PyString_AsString(pValue)); Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr, "Call failed\n"); return 1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", function); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", file); return 1; } Py_Finalize(); return 0; }

Python 使用C+调用sympy+;嵌入 我试图使用Calm和C++程序,并使用下面的嵌入代码来完成。p> int sym_ex(const char * input_expression, const char * output_expression){ PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; const char * file = "sympy"; const char * function = "simplify"; Py_Initialize(); //PyRun_SimpleString("from sympy import *\n"); pName = PyString_FromString(file); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, function); /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New(1); pValue = PyString_FromString(input_expression); printf("the string passed %s\n", input_expression); if (!pValue) { Py_DECREF(pArgs); Py_DECREF(pModule); fprintf(stderr, "Cannot convert argument\n"); return 1; } /* pValue reference stolen here: */ PyTuple_SetItem(pArgs, 0, pValue); pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { printf("Result of call: %s\n", PyString_AsString(pValue)); Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr, "Call failed\n"); return 1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", function); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", file); return 1; } Py_Finalize(); return 0; },python,c++,sympy,python-embedding,Python,C++,Sympy,Python Embedding,但是,当我用这个函数编写一个小程序并运行它时 #include <stdio> #include <iostream> int main(){ char * input_expression = "2 + 2"; char * output_expression = new char[250]; sym_ex(input_expression, output_expression); return 0; } #包括 #包括 int main(){ char*in

但是,当我用这个函数编写一个小程序并运行它时

#include <stdio>
#include <iostream>

int main(){

char * input_expression = "2 + 2";
char * output_expression = new char[250];

sym_ex(input_expression, output_expression);

return 0;
}
#包括
#包括
int main(){
char*input_expression=“2+2”;
字符*输出_表达式=新字符[250];
sym_ex(输入_表达式、输出_表达式);
返回0;
}
它给出了以下类型错误。我在VisualC++中运行,Python 2.7.8

 Exception TypeError: 'expected string or Unicode object, Integer found' in <module 'threading' from 'C:\Python278\Lib\threading.pyc'> ignored
异常类型错误:忽略中的“预期字符串或Unicode对象,找到整数”

有人能帮我吗?谢谢

这里的问题是调用
String\u AsString(pValue)

sympy.simplify(“2+2”)
的结果是4,这是一个整数而不是字符串。结果是Python引发了一个
TypeError
异常


如果您想要一个字符串作为结果,您可能应该首先调用
PyObject\u Str(pValue)
,然后将其转换为C字符串。

更大的目标是什么?我不清楚你为什么要这么做。是为了速度吗?那么CcSyMy可能是你想要的……CcSmiy在Visual C++(微软编译器)下编译吗?我不知道。看起来他们使用了CMake,所以很有可能:
printf("Result of call: %s\n", PyString_AsString(pValue));