[Pyrex] Compiling in cpp mode

Serge Barbosa Da Torre sergiobdt at yahoo.com
Mon May 24 19:08:51 CEST 2004


Hi,
For reference, here is a quick dirty solution for being able to run pyx in
cpp mode with the MS VC6 compiler.

By default Pyrex convert the pyx file to .c
By using the --swig-cpp flag (as in a makefile: python Setup.py
build_ext --inplace --swig-cpp), one can force the generation of cpp files
instead.
Pyrex distutil extension tries to perform some basic cleaning of the
generated cpp file, however:

  A. there is a problem with the staticforward keyword which is defined as:
#define staticforward  static  (in Python/include/object.h).
In the case of MSVC, one needs to define it as an extern instead.
My temporary solution is to include a compatcpp.h  file on top of the pyrexc
files as followed:
// top of the pyx file
cdef extern from "compatcpp.h":
     pass

with:
// File compatccp.h
#ifndef COMPATCPP_H
#define COMPATCPP_H

#ifdef _MSC_VER
#  ifndef __cplusplus  // Supporting in c mode with cross compilation with
cpp
#    define inline  __inline   // VC++ do not recognize inline as a C
keyword
#  else                // Supporting in cpp mode
#    undef staticforward
#    undef statichere
#    define staticforward extern
#    define statichere static
#  endif
#endif
#endif // COMPATCPP_H

 B. pyrexc adds the macro directive on the first line: "extern "c" { } of
the
    generated cpp file.
    This breaks in VC++. It should be placed after the list of #include.
    Patch need to be applied to Pyrex/Distutil/build_ext.py :
    f = open(filename, 'w')
    # SBDT - Hack: I change the original code so that the external "C" is
    # placed after all the includes
    k = 0
    while 1:
        if lines[k].startswith("typedef"): break
        k+=1
    lines.insert(k, 'extern "C" {\n')
    #end hack





More information about the Pyrex mailing list