Python C API: T_INT 未在此范围内声明
Python C API: T_INT was not declared in this scope
我正在跟随 the official tutorial of Python API 为 Python 在 C++ 中创建一个简单的扩展类型。但是我无法成功编译我的代码。因为当我在我的代码中使用 T_INT
时,我得到一个错误 'T_INT' was not declared in this scope
。我忘了什么吗?我在教程中找不到答案。
这是我的 C++ 代码:
#define PY_SSIZE_T_CLEAN
#include <python3.6/Python.h>
#include <stddef.h>
typedef struct {
PyObject_HEAD
int num;
} MyObject;
static PyMemberDef MyMembers[] = {
{ "num", T_INT, offsetof(MyObject, num), 0, NULL },
{ NULL }
};
static PyTypeObject MyType = []{
PyTypeObject ret = {
PyVarObject_HEAD_INIT(NULL, 0)
};
ret.tp_name = "cpp.My";
ret.tp_doc = NULL;
ret.tp_basicsize = sizeof(MyObject);
ret.tp_itemsize = 0;
ret.tp_flags = Py_TPFLAGS_DEFAULT;
ret.tp_new = PyType_GenericNew;
ret.tp_members = MyMembers;
return ret;
}();
static PyModuleDef moddef = []{
PyModuleDef ret = {
PyModuleDef_HEAD_INIT
};
ret.m_name = "cpp";
ret.m_doc = NULL;
ret.m_size = -1;
return ret;
}();
PyMODINIT_FUNC
PyInit_cpp(void)
{
PyObject *mod;
if (PyType_Ready(&MyType) < 0)
return NULL;
mod = PyModule_Create(&moddef);
if (mod == NULL)
return NULL;
Py_INCREF(&MyType);
PyModule_AddObject(mod, "My", (PyObject *)&MyType);
return mod;
}
我用下面的命令编译:
g++ -std=c++11 -shared -fPIC -o cpp.so tt.cpp
我得到的第一个错误是:
tt.cpp:10:11: error: 'T_INT' was not declared in this scope
我的g++
版本是7.3.0
是的,您忘记了一些东西,特别是教程中包含的两个内容中的第二个:
#include "structmember.h"
那就是提供 T_INT
的那个。 (您可能在 python3.6/structmember.h
中拥有它,查看您现有的导入。)
我想你忘了在你的代码中添加另一个 include 语句。
你能试试包括吗
#include "structmember.h"
在您的代码中(也许您需要搜索 header)
#include <python3.6/Python.h>
在您的代码中,您可能必须这样做
#include python3.6/structmember.h
同样的问题还有另一个答案 here。
我正在跟随 the official tutorial of Python API 为 Python 在 C++ 中创建一个简单的扩展类型。但是我无法成功编译我的代码。因为当我在我的代码中使用 T_INT
时,我得到一个错误 'T_INT' was not declared in this scope
。我忘了什么吗?我在教程中找不到答案。
这是我的 C++ 代码:
#define PY_SSIZE_T_CLEAN
#include <python3.6/Python.h>
#include <stddef.h>
typedef struct {
PyObject_HEAD
int num;
} MyObject;
static PyMemberDef MyMembers[] = {
{ "num", T_INT, offsetof(MyObject, num), 0, NULL },
{ NULL }
};
static PyTypeObject MyType = []{
PyTypeObject ret = {
PyVarObject_HEAD_INIT(NULL, 0)
};
ret.tp_name = "cpp.My";
ret.tp_doc = NULL;
ret.tp_basicsize = sizeof(MyObject);
ret.tp_itemsize = 0;
ret.tp_flags = Py_TPFLAGS_DEFAULT;
ret.tp_new = PyType_GenericNew;
ret.tp_members = MyMembers;
return ret;
}();
static PyModuleDef moddef = []{
PyModuleDef ret = {
PyModuleDef_HEAD_INIT
};
ret.m_name = "cpp";
ret.m_doc = NULL;
ret.m_size = -1;
return ret;
}();
PyMODINIT_FUNC
PyInit_cpp(void)
{
PyObject *mod;
if (PyType_Ready(&MyType) < 0)
return NULL;
mod = PyModule_Create(&moddef);
if (mod == NULL)
return NULL;
Py_INCREF(&MyType);
PyModule_AddObject(mod, "My", (PyObject *)&MyType);
return mod;
}
我用下面的命令编译:
g++ -std=c++11 -shared -fPIC -o cpp.so tt.cpp
我得到的第一个错误是:
tt.cpp:10:11: error: 'T_INT' was not declared in this scope
我的g++
版本是7.3.0
是的,您忘记了一些东西,特别是教程中包含的两个内容中的第二个:
#include "structmember.h"
那就是提供 T_INT
的那个。 (您可能在 python3.6/structmember.h
中拥有它,查看您现有的导入。)
我想你忘了在你的代码中添加另一个 include 语句。
你能试试包括吗
#include "structmember.h"
在您的代码中(也许您需要搜索 header)
#include <python3.6/Python.h>
在您的代码中,您可能必须这样做
#include python3.6/structmember.h
同样的问题还有另一个答案 here。