- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
コピーされたリンク
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
f_func does not exist in anything you posted c_func does. Posting a proper example that show the problem would be useful.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
sorry for that
I finally resolved the problem. If this can be useful I can post a full working example
best
jac
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
