c++ - How to call Fortran dll with function as argument in C -
i use vs2013 , intel visual fortran, make dll fortran code. in fortran subroutine, has 3 arguments, "fa" passing function, "a" passing array, "b" passing number. doesn't work when call in vc++.
update:
well... program above chaotic, remodify program below. create fortran dll can pass array sum elements , call c function.
module callctest use, intrinsic :: iso_c_binding implicit none private public callcfun contains subroutine callcfun(a,b) bind(c,name = "callcfun") !dec$ attributes dllexport :: callcfun implicit none real(c_double), dimension(*):: !receive array c type(c_funptr), value :: b ! receive c function real, parameter::pi=3.14159 integer :: real :: sum = 0.0 = 1,3 ! sum array elements sum = sum+a(i) end write(*,*) 'total',sum return end subroutine callcfun end module callctest
and c code call fortran subroutine:
#include "stdafx.h" #include <iostream> using namespace std; extern "c" { void callcfun( double[], void(f)(int)); // } void sayhello(int a){ cout << "hi! fortran " <<a<< endl; } int main(){ double a[] = { 10.0, 50, 3.0 }; callcfun(a, sayhello(50)); system("pause"); return 0; }
in c program, function "sayhello" print words "hi! fortran" , integer, want call sayhello functionby calling "callcfun( array, function )" written fortran.
it works when call "callcfun( array, function )" in c program if function "sayhello" print "hi! fortran". add int argument sayhello function print words "hi! fortran" , integer argument. function "callcfun" doesn't execute successfully.
error list:
error 1 error c2664: 'void callcfun(double [],void (__cdecl *)(int))' : cannot convert argument 2 'void' 'void (__cdecl *)(int)' c:\users\emlab\documents\visual studio 2013\projects\vc++\call_for_ex\call_for_ex\call_for_ex.cpp 21 1 call_for_ex
2 intellisense: argument of type "void" incompatible parameter of type "void (*)(int)" c:\users\emlab\documents\visual studio 2013\projects\vc++\call_for_ex\call_for_ex\call_for_ex.cpp 21 14 call_for_ex
errors show problem in c function, how pass c function fortran dll call in fortran?
for fortran procedure interoperable, bind(c) attribute required. both fortran procedures missing attribute.
for procedure exported dll on windows, symbolic name of procedure must supplied linker. (there 3 ways of doing this: source directive passed through compiler object code (as per !dec$ attributes dllexport
directive used arraysum
procedure), listing procedure name in module definition file or listing name after /export on linker command line - assuming 2 latter methods have not been used.) given source directive approach of nominating 1 procedure has been used 1 of procedures (arraysum
) exported, method should used other procedure (fcn
) needs exported.
for c++ function have c linkage, must declared extern "c"
. there no such declaration fcn
in c++ code. (the absence of declaration fcn means c++ code should not have compiled.)
the import library (.lib) generated linker when building fortran dll must provided linker when exe built c++ code. typically done within visual studio providing import library "additional dependency" on linker > input property page.
Comments
Post a Comment