Fortran

Fortran, as derived from Formula Translating System, is a general-purpose, imperative programming language. It is used for numeric and scientific computing.

  1. Fortran Programming Language

  2. Fortran Tutorial

  3. Getting started with gfortran (PDF)

  4. gfortran(1) — Linux manual page

  5. Using GNU Fortran

  6. INTRODUCTION TO FORTRAN

  7. GNU Fortran compiler (gfortran)

  8. LINK : fatal error LNK1104: cannot open file ‘ifmodintr.lib’

  9. Configuring Visual Studio for Mixed-Language Applications

  10. Fortran for C/C++ developers made easier with CMake

  11. Using C/C++ and Fortran together

  12. Calling Fortran From C - Part 1: Hello, World

  13. Calling Fortran From C - Part 2: A Simple Function

  14. Calling Fortran From C - Part 3: Arrays and Matrices

  15. Fortran与C/C++混合编程示例

  16. C与Fortran混合编程

  17. Visual Studio 中 Fortran 和 C 的混合编程

  18. Mix C Fortran

  19. C-Fortran 接口

  20. Fortran-C-CPP混合编程-1

  21. 混合 Fortran 和 C++

  22. Windows系统下Fortran编程

  23. VS2019中C++与Fortran的混合编程

  24. Fortran C/C++ interoperability

  25. A Modern Fortran Scientific Programming Ecosystem

  26. Intel® C++ & Fortran Compiler

  27. Fortran Programming Tutorials (Revised)

  28. Compiling C/C++/Fortran code

  29. Fortran with CMake (Simple Tutorial)

  30. Modern Fortran: Concurrency and Parallelism

  31. Parallel programming without MPI - Using coarrays in Fortran

  32. Fortran 90 Module Dependencies

  33. Modern Fortran in Science and Technology

  34. Writing Makefiles for Modern Fortran

  35. Mixing C++ and Fortran

  36. Getting started with Fortran

  37. Visual Studio Code C/C++/Fortran with Multiple Source Files

  38. Using gfortran with external libraries and module files

  39. GfortranApps

  40. Setting Up Windows For Fortran Development

  41. Awesome Fortran

Compiling the source code

But the basics are simple enough. Take the gfortran compiler, part of the GNU compiler collection. To compile a simple program as the one above, that consists of one source file, you run the following command, assuming the source code is stored in the file “hello.f90”:

gfortran -c hello.f90

This results in a file “hello.o” (as the gfortran compiler uses “.o” as the extension for the object files).

The option “-c” means: only compile the source files. If you were to leave it out, then the default action of the compiler is to compile the source file and start the linker to build the actual executable program. The command:

gfortran hello.f90
gfortran hello.f90 -o main

Building Shared Libraries

  1. A.1.1 Building Shared Libraries

  2. Creating FORTRAN Libraries

Fortran Formats

  1. Fortran Formats

Mixed-Programming

  1. CALLING FORTRAN SUBROUTINES FROM FORTRAN, C, AND C++

  2. C-Fortran Interface

  3. 新语法系列 之 interface 功能详解

  4. Calling ‘C’ from FORTRAN

  5. Calling C++ from Fortran

  6. Jean Zay: Calling C functions from Fortran

  7. Writing a Fortran-C interface

  8. Interoperation of Fortran with C

  9. ISO_C_BINDING

  10. Iso_c_binding: Looking for practical example of how it helps with mangling

  11. Interoperability : Calling Fortran from C (i)

  12. Mixing C++ and Fortran

  13. Philip Semanchuk| Python, C, C++, and Fortran Relationship Status: It’s Not That Complicated

  14. Mixing C, C++, and Fortran

  15. Interoperable-Subroutines-and-Functions

  16. Interoperation of Fortran with C

  17. Iso_c_binding: Looking for practical example of how it helps with mangling

  18. Managing libraries (static and dynamic libraries)

  19. More on Calling C from FORTRAN

  20. Mixed C/Fortran Programming

  21. C-Fortran Interface

  22. C structs in Fortran

  23. Using Fortran from C/C++

fortran2018-examples

  1. fortran2018-examples

  2. fortran-cpp-interface

  3. fortran-submodule

  4. Fortran MPI Examples

  5. Fortran Parallel Examples

  6. Sparse Fortran libraries

  7. Object-oriented Fortran NetCDF4 interface

  8. Geospace code

Example1

main.c

extern void hello_print();
int main( int argc, char**argv )
{
      hello_print();
      return 0;
}

sub.c

#include <stdio.h>

void hello_print()
{
    printf("hello!\n");
}

Windows11+oneAPI+icx

cd d:\work\modern_cmake_work\ModernCMake\codes\cmake\add_library\test\07b\build\
$ cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'
$ icx -c ../sub.c
$ icx -c ../main.c
$ icx -o testprj main.obj sub.obj

Windows11+oneAPI+icx static lib

cd d:\work\modern_cmake_work\ModernCMake\codes\cmake\add_library\test\07c\build\
$ cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'
$ icx -c ../sub.c
$ lib /OUT:sub.lib sub.obj
$ icx -c ../main.c
$ icx -o testprj main.obj sub.lib

Windows11+oneAPI+icx static lib+CMake

cmake_minimum_required ( VERSION 3.28 )

project ( testprj )

add_library(sub OBJECT sub.c)

add_executable ( ${PROJECT_NAME}
    main.c
)

target_link_libraries( ${PROJECT_NAME}
    PRIVATE
        sub
)

Windows11+oneAPI+icx static lib+CMake v1

cmake_minimum_required ( VERSION 3.28 )

project ( testprj )

add_library(sub OBJECT sub.c)

add_executable ( ${PROJECT_NAME}
    main.c
)

target_link_libraries( ${PROJECT_NAME}
    PRIVATE
        $<TARGET_OBJECTS:sub>
)

get_target_property(prj_LINK_LIBRARIES ${PROJECT_NAME}  LINK_LIBRARIES)

add_custom_target ( print
    ${CMAKE_COMMAND} -E
    echo
    prj_LINK_LIBRARIES = ${prj_LINK_LIBRARIES} &
    echo
    TARGET_OBJECTS:sub = $<TARGET_OBJECTS:sub>
)

Windows11+oneAPI+icx static lib+CMake v2

cmake_minimum_required ( VERSION 3.28 )

project ( testprj )

add_library(sub OBJECT sub.c)

add_executable ( ${PROJECT_NAME}
    main.c
    $<TARGET_OBJECTS:sub>
)

message ( STATUS "sub = ${sub}" )

get_target_property(prj_LINK_LIBRARIES ${PROJECT_NAME}  LINK_LIBRARIES)

add_custom_target ( print
    ${CMAKE_COMMAND} -E
    echo
    prj_LINK_LIBRARIES = ${prj_LINK_LIBRARIES} &
    echo
    TARGET_OBJECTS:sub = $<TARGET_OBJECTS:sub>
)
cmake ..
cmake --build . --target print
cmake --build . --config Debug --target print
cmake --build . --config Release --target print

Example2

sub.c

#include <stdio.h>

void hello_print()
{
    printf("hello!\n");
}

main.c

extern void hello_print();
int main( int argc, char**argv )
{
      hello_print();
      return 0;
}

fmain.f90

program main
  implicit none

  interface
    subroutine fortran_hello_print() bind(C,name='hello_print')
    end subroutine fortran_hello_print
  end interface

  call fortran_hello_print()

end program main

Windows11+oneAPI+icx+ifort+CMake

cmake_minimum_required ( VERSION 3.28 )

project ( testprj )

add_library(sub OBJECT sub.c)

add_executable ( CPrj
    main.c
)

enable_language(Fortran)

add_executable ( FPrj
    fmain.f90
)

target_link_libraries( CPrj
    PRIVATE
        $<TARGET_OBJECTS:sub>
)

target_link_libraries( FPrj
    PRIVATE
        $<TARGET_OBJECTS:sub>
)

get_target_property(cprj_LINK_LIBRARIES CPrj LINK_LIBRARIES)
get_target_property(fprj_LINK_LIBRARIES FPrj LINK_LIBRARIES)

add_custom_target ( print
    ${CMAKE_COMMAND} -E
    echo
    cprj_LINK_LIBRARIES = ${cprj_LINK_LIBRARIES} &
    echo
    fprj_LINK_LIBRARIES = ${fprj_LINK_LIBRARIES} &
    echo
    TARGET_OBJECTS:sub = $<TARGET_OBJECTS:sub>
)
cmake ..
cmake --build . --target print
cmake --build . --config Debug --target print
cmake --build . --config Release --target print
Run Build Command(s): "C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/devenv.com" IntelFortranImplicit.sln /build Debug /project ALL_BUILD
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\Developer Command Prompt for VS 2022.lnk
%comspec% /k "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"