Calling C++ DLL

Calling C++ DLL

I am having difficulty with the 'Visual Fortran Calling Visual C Example'. The example is working fine as is, but following the instructions in the source file I have created a C++ DLL and get the following Fortran linking error
Compiling Fortran...
N: empFortran testVF Calls VCfmain.f90
Linking...
fmain.obj : error LNK2001: unresolved external symbol _C_ROUTINE@20
Debug/VF Calls VC.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I have Aliased the DLL function ?c_routine@@YAXHPAD0@Z and confirmed the Exported name matches that in the DLL. I have also tried exposing _C_ROUTINE@20 in the C++ DLL, but the same error is raised.

I would also like to convert the Fortran into a DLL. Is it possible for a Fortran DLL to make calls to a C++ DLL? or the Fortran DLL to have C++ routines?

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Judging on the linker error, you did not declare C routine in fmain.f90 properly. It should look like:

 
INTERFACE 
    SUBROUTINE C_ROUTINE(arg1,arg2,arg3,arg4,arg5) 
    !DEC$ATTRIBUTES C, DLLIMPORT, ALIAS: '?c_routine@@YAXHPAD0@Z':: C_ROUTINE 
    END SUBROUTINE 
END INTERFACE 

Btw, it is recommended to declare the C++ functions exported from dll
like (in .cpp and .h file):

extern "C" __stdcall c_routine(...

In this way, you get STDCALL instead of C calling convention and usual name mangling (_c_routine@20 instead of C++ type-safe ?c_routine@XYZ...).

Did you insert dll's .lib file into your fortran project?

HTH

Jugoslav

Jugoslav
www.xeffort.com

Thank you for your reply.
Checking the the C++ DLL I found the routine exposed was _C_ROUTINE@12 not _C_ROUTINE@20. So I have added integer arguments for the two strings passed and now it compiles and nearly works.

The first string argument is fine but the second is wrong. This is the C++ routine prototype.

extern "C"
__declspec(dllexport) void FAR __stdcall C_ROUTINE (int int_arg, char* input_text, int int_input, char* output_text, int int_output);

The Fortran interface is declaired as
INTERFACE
SUBROUTINE c_routine (int_arg, str_in, str_out)
Are the arguments in the right order and finally can I make the Fortran into a DLL aswell.

Thank you again

Terry Hawkes

Leave a Comment

Please sign in to add a comment. Not a member? Join today