[Python][扩展]error: unknown type name ‘Py_ssize_t‘ Py_ssize_t ob_refcnt;

异常记录:

Extest2.c:1:10: fatal error: 'object.h' file not found
#include <object.h>
         ^~~~~~~~~~
1 error generated.
ws@GRPC Extest % gcc Extest2.c -o Extest2 -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/ -lpython3.9
In file included from Extest2.c:1:
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:107:5: error: unknown type name 'Py_ssize_t'
    Py_ssize_t ob_refcnt;
    ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:117:5: error: unknown type name 'Py_ssize_t'
    Py_ssize_t ob_size; /* Number of items in variable part */
    ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:132:49: error: unknown type name 'Py_ssize_t'
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
                                                ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:142:50: error: unknown type name 'Py_ssize_t'
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
                                                 ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:167:22: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
typedef Py_ssize_t (*lenfunc)(PyObject *);
                     ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:167:9: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
typedef Py_ssize_t (*lenfunc)(PyObject *);
~~~~~~~ ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:167:20: error: function cannot return function type 'int (PyObject *)' (aka 'int (struct _object *)')
typedef Py_ssize_t (*lenfunc)(PyObject *);
                   ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:186:21: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
typedef Py_hash_t (*hashfunc)(PyObject *);
                    ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:186:9: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
typedef Py_hash_t (*hashfunc)(PyObject *);
~~~~~~~ ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:186:19: error: function cannot return function type 'int (PyObject *)' (aka 'int (struct _object *)')
typedef Py_hash_t (*hashfunc)(PyObject *);
                  ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:209:23: error: expected function body after function declarator
PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
                      ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:211:23: error: expected function body after function declarator
PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
                      ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:214:19: error: expected function body after function declarator
PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
                  ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:217:23: error: expected function body after function declarator
PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
                      ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:218:24: error: expected function body after function declarator
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
                       ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:219:20: error: expected function body after function declarator
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
                   ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:223:17: error: expected function body after function declarator
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
                ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:227:26: error: expected function body after function declarator
PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
                         ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:228:26: error: expected function body after function declarator
PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
                         ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:229:26: error: expected function body after function declarator
PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
                         ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:231:27: error: expected function body after function declarator
PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
                          ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:233:17: error: expected function body after function declarator
PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
                ^
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/object.h:234:24: error: expected function body after function declarator
PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
                       ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]

解决方案:

在#include "Python.h"头文件前添加PY_SSIZE_T_CLEAN宏:

#define PY_SSIZE_T_CLEAN /* Make “s#” use Py_ssize_t rather than int. */
#include “Python.h”

参考:

1. Extending Python with C or C++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值