Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs have moved to the Altera Community. Existing Intel Community members can sign in with their current credentials.
29304 Discussions

Calling Fortran DLL from C++ using Visual Studio 2008

rocket_man
Beginner
3,812 Views

I am have troubles getting a simple example to make a C++ program call Fortran functions from a Fortran DLL. Here is a description of what I have done, please help me do it right:

1) Created a new Intel Fortran project, a blank DLL (using Visual Studio 2008)

2) wrote a simple Fortran program: (fortfunc.F90)

subroutine FR1(N,M)

M=N*N

return

end

integer function FF1(N)

FF1=N*N*N

return

end

3) I compiled it using visual studio 2008 and it created the appropriate DLL (fortfunc.dll)

4) I created another new project, C++ window32 console blank project

5) wrote a simple C++ program to call the fortran functions

#include

using namespace std;

extern "C" {

void __stdcall FR1(int *a, int *b);

int __stdcall FF1(int *c);

}

int main() {

int n = 10,nSq,nCube;

FR1(&n,&nSq);

cout << "The square is:" << nSq << endl;

nCube=FF1(&n);

cout << "The cube is:" << nCube << endl;

return 0;

}

5) I moved the fortfunc.dll to the same folder where my main.cpp exists

6) from the property->Linker->inputs->Additional Dependencies I added the fortfunc.dll

note: I also tried adding the whole directory path and got the same results

7) now I tried to compile the C++ and here is the error:

>Compiling...

1>main.cpp

1>Linking...

1>fortfunc.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2F8

1>Build log was saved at "file://c:\\projects\\sandbox\\test2\\test2\\Release\\BuildLog.htm"

1>test2 - 1 error(s), 0 warning(s)

My question is pretty simple, what is the correct to add the dll so the C++ can call the fortran functions?

Thank you

0 Kudos
1 Solution
Steven_L_Intel1
Employee
3,812 Views
[fortran]subroutine FR1(N,M)
!DEC$ ATTRIBUTES DLLEXPORT :: FR1

M=N*N

return

end

 

integer function FF1(N)
!DEC$ ATTRIBUTES DLLEXPORT :: FF1

FF1=N*N*N

return

end[/fortran]

Just remove the __stdcall from your C++ code - the rest looks ok to me.

View solution in original post

0 Kudos
12 Replies
Steven_L_Intel1
Employee
3,812 Views
  1. You are missing !DEC$ ATTRIBUTES DLLEXPORT directives for FR1 and FF1
  2. You should not use __stdcall when calling Intel Fortran (unless you have changed the default convention.)
  3. You do not add a .DLL as a linker dependency - you add the .LIB created when the DLL was built. But because of #1, you have no .LIB
0 Kudos
rocket_man
Beginner
3,812 Views

Where do I need to put the !DEC$ ATTRIBUTES DLLEXPORT? and how should I call the fortran functions? (to the best of my knowledge I have not changed any of the default conventions).

Thanks

0 Kudos
Steven_L_Intel1
Employee
3,813 Views
[fortran]subroutine FR1(N,M)
!DEC$ ATTRIBUTES DLLEXPORT :: FR1

M=N*N

return

end

 

integer function FF1(N)
!DEC$ ATTRIBUTES DLLEXPORT :: FF1

FF1=N*N*N

return

end[/fortran]

Just remove the __stdcall from your C++ code - the rest looks ok to me.
0 Kudos
rocket_man
Beginner
3,812 Views

Awesome! That worked.

I would however note that the fortran libraries need to be included when using Visual Studio 2008. Not sure why this doesn't happen automatically but here is what I found worked:

properties->Linker->General->Additional Library Directories->""

0 Kudos
Steven_L_Intel1
Employee
3,812 Views
That's if the main program is not Fortran. See Configuring for Mixed Language Applications.
0 Kudos
polarbear00
Beginner
3,812 Views
I have exactly same question as the one you answered, however when I try to add "Dll1.lib" to below location
***properties->Linker->General->Additional Library Directories->""***
it seeks "folder" and not "file" (so that i can add "Dll1.lib"). I copied both Dll1.lib and Dll1.dll in Debug folder of the VC++ 2010 solution where the .exe file is yet it did not work.
I copied the whole VC++ and IVF codes at the end. when i build it I get below errors:

3 IntelliSense: #error directive: Please use the /MD switch for _AFXDLL builds
1 error LNK2019: unresolved external symbol _Dll1 referenced in function "public: void __thiscall CSDI1View::OnToolsDependent(void)" (?OnToolsDependent@CSDI1View@@QAEXXZ)
2 error LNK1120: 1 unresolved externals



******************************* Compiled in IVF *********
subroutine Dll1(a)
implicit none
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, REFERENCE :: Dll1
!DEC$ ATTRIBUTES ALIAS:'Dll1' :: Dll1
Doubleprecision :: a
a=5.5;
return
end !subroutine Dll1

******************************** Call from VC++ in Visual Studio 2010 *****
#include "iostream"
using namespace std;
....

extern "C" void Dll1(double *a);
void CSDI1View::OnToolsDependent()
{
CDependentDlg Dlg;
Dlg.DoModal();
double a;
a=35;
Dll1(&a);

}

Thanks
0 Kudos
Steven_L_Intel1
Employee
3,812 Views
You want to add the library to "Additional Dependencies". Or, the way I like to do it, add the library as a "source file" to the project.

You don't want to say STDCALL. Try this:

subroutine Dll1(a) BIND(C,NAME="Dll1")
implicit none
!DEC$ ATTRIBUTES DLLEXPORT :: Dll1
Doubleprecision :: a
a=5.5;
return
end !subroutine Dll1

On the C++ side, I'm not really sure what the error is about - perhaps you specified static libraries?
0 Kudos
polarbear00
Beginner
3,812 Views
I just realized that my IVF evalusation license expired and no longer can change the fortran code. I added the .lib as source file and still I am getting same errors in VC++. Here "You don't want to say STDCALL. Try this:" you mean that if I don't want to use _stdcall in VC++ I can change the fortran code the way you recommended?
By the way I am persuaded to buy Latest IVF with MKL and IMSL for the company I work at. I have two computers here though, one laptop windows 7 64 bit and one desktop XP 32 bit. would the IVF be compatible with both?

Thanks
0 Kudos
Steven_L_Intel1
Employee
3,812 Views
I did not see __stdcall in your C++ declaration, so you don't want to use STDCALL in Fortran.

Yes, the Intel compiler works with both of those confgurations.
0 Kudos
polarbear00
Beginner
3,812 Views
Thanks Steve, it was resolved.
0 Kudos
Deb_M_
Beginner
3,812 Views

I am working on a mixed language projoject. I am going to develo an windows form application using C# on visual studio 2010. Would you please help me on the following problems:

1> I have "c" codes whcich  calls fortran functions and sub routines

2> I have fortan functions/subroutines which calls "c" codes  

3> I want to make dll from the above codes and want to use them in my windows form application using C# under visual studio

How do I compile the c and fortran code to make dll ( both 32/64 bit) for using them in C# code. How to setup visual studio 2010. Please help me with two prototype examples (c/fortran c# and fortran/c c# ) which can handle two-dimensional array.

Thanks

Deb

0 Kudos
Steven_L_Intel1
Employee
3,812 Views

Deb, this would be better as a separate topic - calling from C# is very different from C++, and there are additional issues that need to be addressed.

0 Kudos
Reply