Monday, December 8, 2008

Mixed C++ and Fortran programming

This note is for compiling C++ and Fortran code. For general language issues concerning mixed programming, check out these references:
http://www.physiology.wisc.edu/comp/docs/notes/not017.html
http://www.math.utah.edu/software/c-with-fortran.html

All individual sources should be compiled into object files by using the "-c" compiler option. Here is how to link these ".o" files. I assume that the main program is in C and a few Fortran subroutines are called.

In gcc-4.0:

".o" from Fortran code is treated the same as those from C++ code. So just use the normal link command:
  g++ $(C_FLAGS) $(COBJS) $(FOBJS) $(LIBS) -o $(EXE)

In gcc-3.0:

Same as the 4.0 version, but the code must be linked against libg2c:
  g++ $(C_FLAGS) $(COBJS) $(FOBJS) $(LIBS) -lg2c -o $(EXE)

In icc:

Fortran compiler, rather than c++ compiler must be used. The option "-nofor_main" tells the compiler that there is no Fortran main subroutine; "-cxxlib" means to link to the C++ libraries.
  ifort -nofor_main -cxxlib $(COBJS) $(FOBJS) $(LIBS) -o $(EXE)

No comments:

 
Creative Commons License All contents on this page are licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.