- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- You are missing !DEC$ ATTRIBUTES DLLEXPORT directives for FR1 and FF1
- You should not use __stdcall when calling Intel Fortran (unless you have changed the default convention.)
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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->""
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
***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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, the Intel compiler works with both of those confgurations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page