next up previous
Next: Linking Up: Compiled Programming Languages Previous: Types


Fortran (compiling and using Fortran code within C/C++)

  1. compile the fortran routines:  >g77 prog.f -c -ffree-form
    the -ffree-form argument allows you to get away from the very picky fixed-form Fortran requirements (sometimes this doesn't work and you need to remove this option)
  2. in the C++ code, declare the fortran function with _ at the end of the name and pass all arguments by reference, noting that prog.f declares them as non-pointers (if the fortran function is named in all caps, make sure the C/C++ references to it are lower case, otherwise you may fail to link properly
    extern C void functionname_(args...) // declaring the function 
    functionname_(args...)// using the function
  3. compile the C++ code: >g++ -c master.C
  4. link:  >g++ master.o prog.o
  5. you may need to use -lf, -lfortran or -lg2c in the linking


next up previous
Next: Linking Up: Compiled Programming Languages Previous: Types
Chris Paciorek 2012-01-21