Saturday, February 16, 2008

Compile Fortran and C++ together under VS2005

Since the new release of windows vista system, the old mixed-language programming under WS6.0 and Compaq Fortran 6.0 does not work anymore since both compilers are not compatible with the new windows OS system. In order to response to such changes, here I did my first test of the mixed-language programming under VS2005 and Intel Fortran. The example codes are copied from the website. It should be noticed that the calling convention between Compaq Fortran 6.0 and VS6.0 is different from that between Intel Fortran and VS2005. Here is a table shown in Steve's article "Porting applications from Compaq frotran to Intel Fortran" to list the difference calling conventions.



Therefore, in Intel Fortran, we no longer require the __stdcall put ahead of the fortran functions in the C code. Instead, __stdcall should be replaced by __cdecl or simply be removed. The original codes in the website become

(1) The C++ file:

// This illustrates how a Fortran routine and function may be
// called from a main program in C++
#include
extern "C"
{
void FR1(int*,int *);
int FF1(int *);
}
int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);
cout << "The square is:" << ncube="FF1(&n);" style="font-weight: bold;"> (2) The Fortran File:

SUBROUTINE FR1(N,M)
C COMPUTES THE SQUARE OF N, RETURNS IN M
M=N*N
RETURN
END
C
INTEGER FUNCTION FF1(N)
C COMPUTES THE CUBE OF N
FF1=N*N*N
RETURN
END

To build mixed-language the VS2005 project, it is not similar to what we did in VS6.0 where we put both C++ files and fortran files in the same project and then compile them together as a whole. In VS2005, we should separate them into two projects, one for C++ and one form Fortran.
  1. Create a static library package for Fortran files by File > New> Intel Fortran > Library > Static Library when creating the package.
  2. Create a Win32 console application package for C++ files by File > New> Visual C++ > Win32 console application
  3. Create a VS2005 solution by going to File > New > Other Studio solutions > VS solution and add both created packages in.
  4. Add the Fortran and C++ into their packages respectively.
  5. Right-click both packages to confirm their runtime libraries are consistent. For C++, it is at Property > C/C++ > Code Generation > runtime library. For Fortran, it is at Property > Fortran > Libraries and also make sure that "Disable OBJCOMMENT ....." is set to be NO
  6. Add the new created lib file (from the Fortran package) into the solution by add the Fortran package name (FortranName.lib) at Property > Linker > Input > Additional dependence by right-click the C++ package
  7. Add the additional library path for C++ package by going to Tools > Options > Projects and Solutions > Library files and click the white boards to add Intel Fortran library at \IntelFortranRoot\Compiler\Fortran\versioncode\IA32\Lib and the location where the new lib file is generated.
  8. After all these settings, I successfully my first package built under VS2005.

No comments: