[f2py] wrapping C/C++

pearu at cens.ioc.ee pearu at cens.ioc.ee
Mon Feb 28 22:52:55 EET 2005


On Mon, 28 Feb 2005, Burlen wrote:

> Hi, I want to wrap c++ code with f2py, because I need to pass
> multidimensional arrays between python and c and I like the Numeric
> arrays in python.

You can wrap certain C functions with f2py. To wrap C++ codes you would
probably need some C layer that f2py could wrap.

> Does any one have a how to? or some simple example?

Since f2py does not know how to scan C files for signatures, you have to
manually write the corresponding .pyf files yourself.

Different from wrapping Fortran codes, signatures of C codes must
use intent(c) attribute 
(i) for function names
(ii) and for C scalars.
Everything else should be the same. See the example below.

> I have tried my own simple example, but it won't import into python.
> here is everything:

> f2ctest.c
> 
> #include<stdio.h>
> int f2ctest(int num)
> {
>   printf( "Test:%i",num);
>   return -1;  
> }

Now, save the following signature

python module f2c
  interface
    function f2ctest(num)
      intent(c) f2ctest
      integer f2ctest, num
      intent(c) num
    end function f2ctest
  end interface
end python module f2c

to f2c.pyf and execute

$ f2py -c f2c.pyf f2ctest.c

In python:
>>> import f2c
>>> print f2c.f2ctest.__doc__
f2ctest - Function signature:
  f2ctest = f2ctest(num)
Required arguments:
  num : input int
Return objects:
  f2ctest : int

>>> r=f2c.f2ctest(3)
Test:3>>> 
>>> r
-1

HTH,
Pearu




More information about the f2py-users mailing list