Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling C Dlls from fortran

asalterain
Beginner
1,661 Views

using CVF I create a single Working Space with two projects:

ONE call myDLLa C Dll as shown

// myDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

extern "C" {
void __stdcall RUN_PP();
}


//MAIN ENTRY FUNCTION
void __stdcall RUN_PP (){

cout <<" IN Run_PP "<}

and myDLL.def is

LIBRARY myDLL
EXPORTS
RUN_PP

THE OTHER a fotran mai program

program mainFORT

implicit none
! cboincinit (status)
INTERFACE
SUBROUTINE RUN_PP ()

!DEC$ ATTRIBUTES DLLIMPORT :: RUN_PP
!DEC$ ATTRIBUTES C, ALIAS:"RUN_PP" :: RUN_PP


END SUBROUTINE RUN_PP
END INTERFACE

print *, 'Hello World'

CALL RUN_PP()

end program mainFORT

compiling goes OK, but get 2 errors at linking and there fore RUN_PP can not be executed. Anyone could give a hand ???

0 Kudos
5 Replies
anthonyrichards
New Contributor III
1,661 Views

It would certainly help if you posted the linker errors. Have you added your compiled 'C'DLL's .LIB file to the FORTRAN project so that the RUN_PP procedure can be found and linked in to the Fortran code?

Assuming your 'C' compiler does not mangle the name RUN_PP (the extern "C" probably helps here), thenthe following should work

!DEC$ ATTRIBUTES DLLIMPORT :: RUN_PP
!DEC$ ATTRIBUTES STDCALL :: RUN_PP
!DEC$ ATTRIBUTES ALIAS: "RUN_PP" :: RUN_PP

Otherwise examine the 'C'DLL using DUMPBIN to check that RUN_PP is the actual name exposed for the procedure. If it is not, then change the ALIAS to reflect the name in the DLL file. Is it possible that your 'C' compiler has added a leading underscore '_' to the procedure name (_RUN_PP)?

0 Kudos
asalterain
Beginner
1,661 Views

Yes, using the option add files I added the file myDLL.lib to the proyect and also I placed, both the myDLL.dll and the myDLL.lib files into the same directory as fortran directory is, also I have tried the option of making the fortran programme dependent on the myDLL.

This is the error I get

Linking...
mainFORT.obj : error LNK2001: unresolved external symbol RUN_PP
Debug/mainFORT.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

mainFORT.exe - 2 error(s), 0 warning(s)

0 Kudos
anthonyrichards
New Contributor III
1,661 Views
Its an exported name problem.
I compiled the following using Visual C++ (note I use __declspec(dllexport) to
avoid use of a DEF file):
// mycdll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
// myDLL.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
return TRUE;
}
extern "C" {__declspec(dllexport) void __stdcall RUN_PP();
}
// MAIN ENTRY FUNCTION
void __stdcall RUN_PP ()
{
cout <<" IN Run_PP "<}
Then I tried DUMPBIN on the export library mycdll.lib and found the following:
C:Documents and Settings>dumpbin /exports mycdll.lib
Microsoft COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file mycdll.lib
File Type: LIBRARY
Exports
ordinal name
 _RUN_PP@0
 Summary
C3 .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6
So, the C++ exported a name with a leading underscore 
added and added a @0 decoration to the end.
Changing the Fortran ALIAS to reflect this to ALIAS :'_RUN+PP@0'
allowed the linking to go ahead OK and the program executed
as expected, giving the following console output:

Hello World
IN Run_PP
Press any key to continue
Things may change if you alter the C-program file type from mycdll.cpp
to mycdll.C and use a 'C' compiler, in which case the name decoration
may change and a modified ALIAS directive may be required.
0 Kudos
asalterain
Beginner
1,661 Views

Yes you are rigth,

after lunch I modified the alias from the value thatwas given by DUMPBIN and it worked. Thankyou so much for your help.

0 Kudos
gtarbell
Beginner
1,661 Views

Hi, I am working on trying to get netCDF(a c dll) to compile on windows, then call it with fortran 90. I was wonding if you could email me the source code you used to do this, including the visual studio solution files for both the C and the fortran.

Thanks,
Geddy Tarbell
University of Montana
geddy.tarbell@umontana.edu

0 Kudos
Reply