hidden text to trigger early load of fonts ПродукцияПродукцияПродукцияПродукция Các sản phẩmCác sản phẩmCác sản phẩmCác sản phẩm المنتجاتالمنتجاتالمنتجاتالمنتجات מוצריםמוצריםמוצריםמוצרים
Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28988 ディスカッション

interface to overloaded subroutine

JacobB
新規コントリビューター I
826件の閲覧回数

Hello 

I have a subroutine that call a "c" function that reads from a binary file.

the fortran subroutine is overloaded:

for example 

subroutine bread(integer)

subroutine bread(double precision)

subroutine bread(char*)

 

how can i define the interface so it covers all the options ?

thanks

jac

 

0 件の賞賛
1 解決策
JacobB
新規コントリビューター I
574件の閲覧回数

the problem:

I have a function written in c that writes data into a binary file. Here is the function header 

void cread (int *unit , char *array , int *ilen , int *ierr);

this function actually writes bytes of data and it can be used to write data of different types (strings, scalars and arrays either integer, single precision of double precision). 

I call this function from a fortran program. Due to the fact that I use the same function in order to write several types of data, I needed to define an interface of overloaded functions as in the attached file.

each overloaded function writes a specific type of data (string, array of doubles, double, etc).

thanks for your help

jac

 

 

 

元の投稿で解決策を見る

9 返答(返信)
andrew_4619
名誉コントリビューター III
767件の閲覧回数

Maybe this example helps. The args need to be unambiguous to the compiler so it knows which version to use.

 

    program iface
    interface a
      subroutine ai(x)
        integer :: x
      end subroutine ai
      subroutine ar(x)
        real :: x
      end subroutine ar
    end interface
    call a(0.0)    !call real version
    call a(3)      !call integer version
    end program iface
    
    subroutine ai(x)
        integer :: x
        write(*,*) 'integer',x
    end subroutine ai
    
    subroutine ar(x)
        real :: x
        write(*,*) 'real',x
    end subroutine ar

 

JacobB
新規コントリビューター I
724件の閲覧回数

hi and thanks for your help 

my case is a litle bit more complex

i have c function  c_func(int *u, char *v, int *n)

 

on the fortran side i have the following overloads

subroutine f_func(iunit,v,n) where v can be an array of real, int or character*

i created an interface as follows

 

interface f_func

subroutine f_func_char(iunit,vc,n)bind (c,name="c_func")

use intrinsic :: iso_binding , only :c_int,c_char

integer(kind=c_int),intent (in), value :: iunit

character(kind=c_char) , intent(out) :: vc(*)

end subroutine f_func_char

 

subroutine f_func_float(iunit,vf,n) bind(c,name="c_func")

use intrinsic :: iso_binding , only :c_int,c_char

integer(kind=c_int),intent (in), value :: iunit

real(kind=kind(1.0)) , intent(out) ,dimension(*) :: vf

end subroutine f_func_float

...

end interface f_func

 

when i compile that i get an error 

thanks

jac

 

andrew_4619
名誉コントリビューター III
712件の閲覧回数

Maybe the code above is not the exact code you tried, it has syntax and spelling issues  ( use, instrinsic - missing comma) and iso_c_binding. Anyway the code below compiles inserted into my earlier example compiles OK. Please use markup on the menu bar to post code, it is much more readable,

    interface f_func
        subroutine f_func_char(iunit,vc,n)bind (c,name="c_func")
            use, intrinsic :: iso_C_binding , only :c_int,c_char
            integer(kind=c_int),intent (in), value :: iunit
            character(kind=c_char) , intent(out) :: vc(*)
        end subroutine f_func_char
        subroutine f_func_float(iunit,vf,n) bind(c,name="c_func")
            use, intrinsic :: iso_c_binding , only :c_int,c_char
            integer(kind=c_int),intent (in), value :: iunit
            real(kind=kind(1.0)) , intent(out) ,dimension(*) :: vf
        end subroutine f_func_float
    end interface f_func

 

JacobB
新規コントリビューター I
703件の閲覧回数

hi 

thanks for your prompt response 

the interface part compiles without errors.

the fortran code that calls this function under different contexts does not.

the error I get is 

error #6285: there is no matching specific subroutine for this generic subroutine call [f_func]

thanks

jac

 

andrew_4619
名誉コントリビューター III
700件の閲覧回数

f_func does not exist in anything you posted c_func does. Posting a proper example that show the problem would be useful.

JacobB
新規コントリビューター I
656件の閲覧回数

sorry for that 

I finally resolved the problem. If this can be useful I can post a full working example 

best

jac

 

Steve_Lionel
名誉コントリビューター III
628件の閲覧回数

Please do explain what you did to resolve it and post a final example. This may help others.

JacobB
新規コントリビューター I
575件の閲覧回数

the problem:

I have a function written in c that writes data into a binary file. Here is the function header 

void cread (int *unit , char *array , int *ilen , int *ierr);

this function actually writes bytes of data and it can be used to write data of different types (strings, scalars and arrays either integer, single precision of double precision). 

I call this function from a fortran program. Due to the fact that I use the same function in order to write several types of data, I needed to define an interface of overloaded functions as in the attached file.

each overloaded function writes a specific type of data (string, array of doubles, double, etc).

thanks for your help

jac

 

 

 

JacobB
新規コントリビューター I
574件の閲覧回数

sorry for the typo cread reads data but the concept is the same

 

返信