[Pyrex] Compiling with a C++ compiler

Andreas Kostyrka andreas at mtg.co.at
Thu Oct 9 09:40:57 CEST 2003


Hi!

Greg asked how to replace staticforward in C++. As I'm replying from the
office I cannot reply directly to his message and have to break the
thread :(

I've got good news. It's possible ;)
I've got bad news, it's not completly trivial :(, as static seems to be
not supported for this anymore. (My german Stroustrup translation at the
office says something like static should only be used inside classes and
functions :( )

Ok, I'm compiling cpptest.pyx
-- cut here
def Hello():
    print "Hello World!"

cdef class Test:
    pass
-- cut here

The resulting .c file must be patched as follows: (patch is attached)

1.) move all includes to the top.
    Motivation: we want all includes to happen outside the anonymous C++
    namespace.

2.) #define static
    #undef staticforward
    #define staticforward extern
    #undef statichere
    #define statichere 

    We do not need static "bindings" anymore ;)
    (and they are ill-defined in C++ anyway, as the same book claims
    that one should use static if one cannot use a namespace.)

3.) Wrap a namespace { ... } around the code.
    This creates an anonymous namespace, in fact it makes all the
    stuff inside "static" in as far that it has compilation unit scope.

4.) #undef DL_EXPORT
    #define DL_EXPORT(x) extern "C" x

    This way (the spec is for Linux, DL_EXPORT should actually hide
    platform specific stuff I guess), all exported functions are visible
    from C and is linkable :)

ad 3.) as a more general notation my copy of Stroustrup seems to claim
    that one can reference an earlier anonymous namespace by $$$. So it
    might be possible (and probably is desirable if public declarations
    are handled) to stop the local namespace and resume it.
    Alternativly, one could use a named Namespace called after the
    module generated.

Greg: Is this enough information for you?

Andreas




More information about the Pyrex mailing list