Fortran support in Code::Blocks

From CodeBlocks
Revision as of 06:22, 4 April 2015 by Briedis (Talk | contribs) (Fortran in Code::Blocks)

Jump to: navigation, search

Presently Code::Blocks IDE has support not only for C/C++ but for Fortran also. Features of Code::Blocks useful for Fortran:

  • Editor with syntax highlighting for fixed and free Fortran source code
  • Support for GFortran and PGI Fortran compilers. Other Fortran compilers can be added by users too. “Support for compiler” means: (a) the graphical interface for the options of the compiler and (b) catching of compilation errors and warnings and jumping to the corresponding code line
  • During the compilation Code::Blocks cares about the dependencies between Fortran files in the project (it is important when Fortran modules are used)
  • Symbol browser with program units and variables defined in Fortran files
  • Code-completion
  • Call-tips
  • and some more.

Most of the functionality useful for Fortran is implemented through FortranProject plugin. More information about the use of Code::Blocks with Fortran can be found here here.

Adding new Fortran file type

It makes sense to add a new "Fortran Sources" filetype to have the Fortran source code files being seperated. Steps to do so:

  • Goto menu "Project" -> "Project tree" -> "Edit file types categories"
  • Click on "Add" to add a new type of files
  • Select "Fortran Sources" as group name
  • Enter the following in the file-mask (without quotation marks): "*.f;*.f77;*.cmn;*.inc;"
  • Click OK to close the dialog.

For your interest: "*.f;*.f77" are normally the default extensions for source code files, "*.cmn" is the extension for Fortran common-blocks and "*.inc" if often used as include file with e.g. variable declarations.

Setting up Fortran projects

To work with Fortran files, do the following: Add a (new) file to the project, saved as "your_fortran_file.f". Verify it's recognised as "Fortran Sources" file type. If not verify you have done the steps above correctly. To enable compiling the project which includes Fortran files follow these steps:

  • Right click on the project, select "Build options" and make sure you have selected the "GNU G77 compiler" (see steps above).
  • Right click on the "your_fortran_file.f" file and select "Properties".

1.) Enable "Compile file" and "Link file".

2.) Under the "Advanced" tab enter "CC" as compiler variable

  • Compile the project, it should work by now.


For troubleshooting enable the "Full command line" Under menu "Settings" -> "Compiler" -> "Other" tab, "Compiler logging". If you see that still the gcc compiler is invoked, make sure you setup the right compiler in the build options for your project AND your target (e.g. "default").

Using gfortran instead of g77

The preceding steps can also be used to set up Code::Blocks to use the GNU gfortran Fortran '95 compiler, albeit with the following changes:

  • Setup the compiler as follows:

1.) The "Compiler's installation directory" should point to the location of the root directory of the gfortran installation, for example if gfortran.exe is located in C:\gfortran\bin the path should read "C:\gfortran" (minus quotes).

2.) Under the "Programs" tab, select "gfortran.exe" as both the C compiler and the linker for dynamic libs instead of "g77.exe"

3.) DO NOT add the linker commands as given previously; leave these fields blank.

  • Adding new Fortran file type

Proceed as above, but add "*.f95" as a file type instead of "*.f77".

Follow the rest of the steps above and gfortran should work.

See also