Calling C function from Fortran

34 views
Skip to first unread message

Wonjun, Choi

unread,
Jul 18, 2011, 5:09:23 AM7/18/11
to Fwrap Users
I'm trying to practice Fwrap and Cython. and I have some question
about calling C function from Fortran.
I had learned Shrubbing sample(related to communication pyx).
and there are two file (Shrubbing.so derived from C, Landscaping.so
derived from another C source)

and also I tested functions example in Fwrap package.
I wonder that when I want to combine C and Fortran, whether I make
two .so file or not.

why don't I write both language in one pyx file?

so I tested below code:

for wrapping C
# mymath.h
double sinc(double);

# mymath.c
# include "math.h"

double sinc(double x) {
return x == 0 ? 1 : sin(x)/x;
}

for wrapping Fortran
"""
The test module was generated with Fwrap v0.1.1.

Below is a listing of functions and data types.
For usage information see the function docstrings.

Functions
---------
func(...)

Data Types
----------
fw_character
fwi_integer

"""
np.import_array()
include 'fwrap_ktp.pxi'
cdef extern from "string.h":
void *memcpy(void *dest, void *src, size_t n)
cpdef api object func(fwi_integer_t a):
"""
func(a) -> (fw_ret_arg,)

Parameters
----------
a : fwi_integer, intent in

Returns
-------
fw_ret_arg : fwi_integer, intent out

"""
cdef fwi_integer_t fw_ret_arg
cdef fwi_integer_t fw_iserr__
cdef fw_character_t fw_errstr__[fw_errstr_len]
func_c(&fw_ret_arg, &a, &fw_iserr__, fw_errstr__)
if fw_iserr__ != FW_NO_ERR__:
raise RuntimeError("an error was encountered when calling the
'func' wrapper.")
return (fw_ret_arg,)


cdef extern from "mymath.h":
double sinc(double)

def call_sinc(x):
return sinc(x)

def printpython(x):
print sinc(x)


but when I had typed the command "import test", the error message
occured.
test.pyx:46:14: undeclared name not builtin: sinc

am I wrong? please recommend your advice!
Reply all
Reply to author
Forward
0 new messages