[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 
         ^~~~~~~~~~
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++

你可能感兴趣的:(Python,python,算法,开发语言)