Add includes to generated header files
I'm working on some C++ projects and I'm using cython to embed python code. In an effort to make things easy for C++ devs not as familiar with cython/python I've made a small change to insert #includes in generated header files the same as is done in generated c code. This allows one who is using code processed with cython to just include the header and not worry about any other headers that may be required to match data types. Not having these headers is especially problematic because otherwise the error message presented at compilation can sometimes be quite cryptic. Patch is attached. Any reason this should not be done? Thanks, Jeff
Jeff Copeland, 03.08.2012 02:18:
I'm working on some C++ projects and I'm using cython to embed python code. In an effort to make things easy for C++ devs not as familiar with cython/python I've made a small change to insert #includes in generated header files the same as is done in generated c code. This allows one who is using code processed with cython to just include the header and not worry about any other headers that may be required to match data types.
Not having these headers is especially problematic because otherwise the error message presented at compilation can sometimes be quite cryptic.
Patch is attached. Any reason this should not be done?
Hmm, didn't try your patch, but I guess it inserts includes for *all* header files that the module that exports the C-API uses internally, right? If so, I'm not sure that's always wanted. I see the advantage, sure, but it may expose a lot of implementation details that external code may not normally have to care about. On the other hand, if extension types are exported, for example, it's somewhat unlikely that their struct would compile without at least some of those header files, because they almost always wrap some kind of externally defined struct, pointer or whatever kind of other data type. That sounds to me like we should make it a configurable option, something like "capi_reexport_cincludes". Stefan
On Thu, Aug 2, 2012 at 8:43 PM, Stefan Behnel <stefan_ml@behnel.de> wrote:
Jeff Copeland, 03.08.2012 02:18:
I'm working on some C++ projects and I'm using cython to embed python code. In an effort to make things easy for C++ devs not as familiar with cython/python I've made a small change to insert #includes in generated header files the same as is done in generated c code. This allows one who is using code processed with cython to just include the header and not worry about any other headers that may be required to match data types.
Not having these headers is especially problematic because otherwise the error message presented at compilation can sometimes be quite cryptic.
Patch is attached. Any reason this should not be done?
Hmm, didn't try your patch, but I guess it inserts includes for *all* header files that the module that exports the C-API uses internally, right?
Yes, you've got it. Certainly quick and dirty to satisfy my immediate need.
If so, I'm not sure that's always wanted. I see the advantage, sure, but it may expose a lot of implementation details that external code may not normally have to care about.
On the other hand, if extension types are exported, for example, it's somewhat unlikely that their struct would compile without at least some of those header files, because they almost always wrap some kind of externally defined struct, pointer or whatever kind of other data type.
Exactly.
That sounds to me like we should make it a configurable option, something like "capi_reexport_cincludes".
I like that thought. The other thing I'd though of is perhaps some syntax in the pyx itself to designate an include file as one that should show in a generated header. When I was looking for a solution to the issue that's what I started looking for in the documents but of course came up empty. I figured it may be something like the case for inclusion in the c file but with a public/api keyword thrown in: cdef extern public from "spam.h": pass Syntax change would probably be more involved. I'll have a go at doing a patch with the option added. Do you mean as a command-line option to cython? Jeff
On Sat, Aug 11, 2012 at 10:27 AM, Jeff Copeland <mongi3@gmail.com> wrote:
On Thu, Aug 2, 2012 at 8:43 PM, Stefan Behnel <stefan_ml@behnel.de> wrote:
That sounds to me like we should make it a configurable option, something like "capi_reexport_cincludes".
Do mean as a command line option, or something else like a compiler directive?
I went ahead and added a command line option. https://github.com/cython/cython/pull/142
participants (2)
-
Jeff Copeland -
Stefan Behnel