Using g95 with MS Visual C++

Usually I use Intel Fortran with MS Visual C++. Today I wanted to check, if it possible to replace Intel Fortran by g95. The goal would be to use g95 to compile a Fortran code but then use cl to link it with C/C++ code.

1) Install

http://www.g95.org/downloads.shtml

Self-extracting Windows x86 (version of 2010-04-28): g95-MinGW.exe.

Double-click the file, and then follow instructions. It is not necessary to install Mingw tools and libraries. Whether to set-up environment variables, this is up to you. I have not done it at this stage. Below I assume that g95 was installed in c:\bin\g95. If it is not the case, please change this path below as needed.

2) Configuration

Under g95/lib/gcc-lib/i686-pc-mingw32/4.x.x there are libf95.a and libgcc.a. It is necessary to rename them as libf95.lib and libgcc.lib. I have put the renamed libraries into g95/lib to simplify things.

Now it is necessary to add g95/bin to PATH and g95/lib to LIB. PATH will be used to find g95, and LIB is used by cl to find libraries. As I am wokring under Cygwin (tcsh), it went as follows

setenv PATH "/cygdrive/c/bin/g95/bin:$PATH"
setenv LIB "$LIB;C:\\bin\g95\lib"

Note that this could be done directly in cmd, I just like tcsh.

3) Test

I have used the code at

http://matrixprogramming.com/files/code/Tools/fortran/

to check everything.

g95 -c sub.f
cl -MD -EHsc -C call.cpp
cl call.obj sub.o libf95.lib libgcc.lib

It is important to mention that one must use -MD. Without this, there is some missing symbol ___p__environ during linking. Otherwise, it is working just fine.


Comments are closed.