How to use the command 'gfortran' (with examples)
The ‘gfortran’ command is a part of the GNU Compiler Collection (GCC) and is used to preprocess and compile Fortran source files, as well as assemble and link them together. It is a powerful tool for compiling and building Fortran programs.
Use case 1: Compile multiple source files into an executable
Code:
gfortran path/to/source1.f90 path/to/source2.f90 ... -o path/to/output_executable
Motivation: When you have a Fortran project that consists of multiple source files, you need to compile them all and link them together to create an executable. This use case allows you to compile multiple source files into a single executable file.
Explanation:
path/to/source1.f90 path/to/source2.f90 ...
: Specifies the paths to the Fortran source files that need to be compiled. You can provide multiple source files separated by spaces.-o path/to/output_executable
: Sets the output file name for the executable that will be generated.
Example output:
The compilation process will start, and once it is finished, an executable file named 'output_executable' will be created in the specified output directory.
Use case 2: Show common warnings, debug symbols in output, and optimize without affecting debugging
Code:
gfortran path/to/source.f90 -Wall -g -Og -o path/to/output_executable
Motivation: When developing a Fortran program, it is important to enable warnings and debug symbols to aid in troubleshooting and debugging. Additionally, you may want to optimize the program without affecting the debugging process. This use case combines these requirements.
Explanation:
-Wall
: Enables displaying common warning messages during compilation.-g
: Includes debug symbols in the output executable, which can be useful for debugging.-Og
: Enables optimization at the debugging level, ensuring optimization does not interfere with the debugging process.-o path/to/output_executable
: Sets the output file name for the executable that will be generated.
Example output:
The compilation process will start, and once it is finished, an executable file named 'output_executable' will be created in the specified output directory. The output executable will include debug symbols and will have undergone optimization at the debugging level.
Use case 3: Include libraries from a different path
Code:
gfortran path/to/source.f90 -o path/to/output_executable -Ipath/to/mod_and_include -Lpath/to/library -llibrary_name
Motivation: Sometimes, you may want to include libraries from a different path to link with your Fortran program. This use case allows you to specify the paths to the libraries to be included.
Explanation:
-Ipath/to/mod_and_include
: Specifies a directory to search for module and include files. This is useful when your Fortran source files have dependencies on modules and include files.-Lpath/to/library
: Specifies a directory to search for library files. This is useful when you have external libraries that need to be linked with your Fortran program.-llibrary_name
: Links the specified library with your Fortran program. Replace ’library_name’ with the actual name of the library.
Example output:
The compilation process will start, and once it is finished, an executable file named 'output_executable' will be created in the specified output directory. The output executable will include the libraries from the provided paths.
Use case 4: Compile source code into Assembler instructions
Code:
gfortran -S path/to/source.f90
Motivation: Sometimes, it’s useful to see the assembly instructions generated from your Fortran source code. This use case allows you to compile the source code into Assembler instructions.
Explanation:
-S
: Generates assembly instructions from the Fortran source code.
Example output:
The compilation process will start, and once it is finished, assembly instructions will be generated for the specified Fortran source code. The output will be in a readable format, showing the low-level instructions generated by the compiler.
Use case 5: Compile source code into an object file without linking
Code:
gfortran -c path/to/source.f90
Motivation: In some cases, you may only want to compile the Fortran source code into an object file without linking it to an executable. This can be useful when debugging or if you plan to link the object file later.
Explanation:
-c
: Compiles the source code into an object file without performing the linking stage.
Example output:
The compilation process will start, and once it is finished, an object file will be created for the specified Fortran source code. The object file can be used later for linking with other object files or libraries.
Conclusion
The ‘gfortran’ command is a versatile tool for compiling and building Fortran programs. By understanding its various use cases, you can efficiently compile, link, and optimize your Fortran source code. Whether you are working with multiple source files, including libraries, or generating assembly instructions, the ‘gfortran’ command is a valuable resource for Fortran developers.