[Cython] Multiple modules in one compilation unit

Dag Sverre Seljebotn dagss at student.matnat.uio.no
Wed Mar 2 11:20:47 CET 2011

This has been raised earlier, but I don't think there was such a 
demonstrative use-case as what I have now.

Fwrap is suppose to be able to wrap Fortran "modules", which is 
essentially a namespace mechanism. It makes sense to convert the 
namespace to Python by creating one Cython pyx file per Fortran module.

However, if you are wrapping a Fortran library this way, you suddenly 
have lots of opportunity to mess up the build:

  - If you build the Fortran code as a static library (rather 
common...), then each pyx file will have their own copy. This will link 
successfully but likely have a rather poor effect.

  - If you link each Cython file with only the corresponding Fortran 
file, things won't work (likely to get missing symbols from cross-module 
calls Fortran-side).

Yes, linking each Cython file to the same shared Fortran library should 
work. Still, this seems dangerous.

  a) Simply make sure to link with shared versions of Fortran libraries 
("this is a documentation problem")

  b) Use some other namespace mechanism in the same pyx (classes with 
static methods...)

  c) Somehow provide more than one module in the same compilation unit. 
Again, this requires the build to work correctly, but seems less 
dangerous, and also has the advantage of *allowing* static linking of 
the Fortran library, if one wants to.

But is something like this possible? Could one implement

cython -o assembly.c file1.pyx file2.pyx file3.pyx

...where assembly.so would contain three Python modules? (initfile1, 
initfile2, and so on...)

Dag Sverre

More information about the cython-devel mailing list