[Pyrex] Questions about forward declarations

Edward C. Jones edcjones at erols.com
Tue May 27 19:57:17 CEST 2003


The Pyrex program "fails.pyx" (below) fails because of the unnecessary 
forward declaration "struct CvSeq". The C program "silly.c" given below 
compiled with gcc. Is the C program correct? What are the Pyrex rules 
about multiple declarations, incomplete declarations, forward 
declarations, etc?

Here is some context. The "struct CvSeq" is machine generated as part of

     struct CvSeq
     cdef struct CvSet:
         CvSeq * h_prev

This code is generated when CvSet is being processed by the code 
generator. Since the generator does not know that CvSeq has already been 
done, it adds the forward declaration "struct CvSeq". It is easier to 
write a code generator if it uses only local information; it is nicer if 
wrapping "CvSet" depends only on

     typedef struct CvSet
     {
         struct CvSeq* h_prev;
     }
     CvSet;

This may be difficult because C has a "one pass" philosophy whereby the 
compiler knows about "CvSeq" while processing "CvSet" but doesn't know 
about "CvSet" while processing "CvSeq". In Python forward declarations 
are not needed. Should Pyrex be like C or like Python?

Thanks,
Ed Jones

---------------------
shrink.h:

typedef struct CvSeq
{
     int i;
}
CvSeq;

typedef struct CvSet
{
     struct CvSeq* h_prev;
}
CvSet;

---------------------
shrink.c:

#include "shrink.h"

---------------------
fails.pyx:

cdef extern from "shrink.h":
     cdef struct CvSeq:
         int  i

     struct CvSeq

cdef class TemP__CvSeq:
     cdef CvSeq* t

     def __new__(self, i):
         sizeof(CvSeq)

---------------------
error message:

fails.pyx:11:8: Cannot take sizeof incomplete type 'struct CvSeq'

---------------------
silly.c:

typedef struct _CvSeq
{
     int i;
}
CvSeq;

struct _CvSeq;

typedef struct CvSet
{
     struct CvSeq* h_prev;
}
CvSet;





More information about the Pyrex mailing list