Fortran
==================================
Fortran, as derived from Formula Translating System, is a general-purpose, imperative programming language. It is used for numeric and scientific computing.
#. `Fortran Programming Language `_
#. `Fortran Tutorial `_
#. `Getting started with gfortran (PDF) `_
#. `gfortran(1) — Linux manual page `_
#. `Using GNU Fortran `_
#. `INTRODUCTION TO FORTRAN `_
#. `GNU Fortran compiler (gfortran) `_
#. `LINK : fatal error LNK1104: cannot open file 'ifmodintr.lib' `_
#. `Configuring Visual Studio for Mixed-Language Applications `_
#. `Fortran for C/C++ developers made easier with CMake `_
#. `Using C/C++ and Fortran together `_
#. `Calling Fortran From C - Part 1: Hello, World `_
#. `Calling Fortran From C - Part 2: A Simple Function `_
#. `Calling Fortran From C - Part 3: Arrays and Matrices `_
#. `Fortran与C/C++混合编程示例 `_
#. `C与Fortran混合编程 `_
#. `Visual Studio 中 Fortran 和 C 的混合编程 `_
#. `Mix C Fortran `_
#. `C-Fortran 接口 `_
#. `Fortran-C-CPP混合编程-1 `_
#. `混合 Fortran 和 C++ `_
#. `Windows系统下Fortran编程 `_
#. `VS2019中C++与Fortran的混合编程 `_
#. `Fortran C/C++ interoperability `_
#. `A Modern Fortran Scientific Programming Ecosystem `_
#. `Intel® C++ & Fortran Compiler `_
#. `Fortran Programming Tutorials (Revised) `_
#. `Compiling C/C++/Fortran code `_
#. `Fortran with CMake (Simple Tutorial) `_
#. `Modern Fortran: Concurrency and Parallelism `_
#. `Parallel programming without MPI - Using coarrays in Fortran `_
#. `Fortran 90 Module Dependencies `_
#. `Modern Fortran in Science and Technology `_
#. `Writing Makefiles for Modern Fortran `_
#. `Mixing C++ and Fortran `_
#. `Getting started with Fortran `_
#. `Visual Studio Code C/C++/Fortran with Multiple Source Files `_
#. `Using gfortran with external libraries and module files `_
#. `GfortranApps `_
#. `Setting Up Windows For Fortran Development `_
#. `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 Dynamic-Link Libraries
----------------------------------
#. `Building Dynamic-Link Libraries `_
There is one more thing to be aware of: On Windows you must explicitly specify that a procedure is to be exported, i.e. is visible in the dynamic library. There are several ways — depending on the compiler you use — to achieve this. One method is via a so-called compiler directive:
::
subroutine myroutine( ... )
!GCC$ ATTRIBUTES DLLEXPORT:: myroutine
Or, with the Intel Fortran compiler:
::
subroutine myroutine( ... )
!DEC$ ATTRIBUTES DLLEXPORT:: myroutine
Sample program: C calling Fortran
#. `Sample program: C calling Fortran `_
#. `Compiling a mixed C-Fortran program (main program is Fortran) `_
#. `C-Fortran Interface- `_
#. `FortranCon2020 [SP]: Shroud: generate Fortran wrappers for C and C++ libraries `_
::
cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'
Temporarily change the value of the environment variable PATH
::
$env:path += ";d:\work\fortran_work\ModernFortran\codes\windows\shared-lib\01\"
Building Shared Libraries
---------------------------------
#. `A.1.1 Building Shared Libraries `_
#. `Creating FORTRAN Libraries `_
Fortran Formats
---------------------------
#. `Fortran Formats `_
Mixed-Programming
------------------------------
#. `CALLING FORTRAN SUBROUTINES FROM FORTRAN, C, AND C++ `_
#. `C-Fortran Interface `_
#. `新语法系列 之 interface 功能详解 `_
#. `Calling 'C' from FORTRAN `_
#. `Calling C++ from Fortran `_
#. `Jean Zay: Calling C functions from Fortran `_
#. `Writing a Fortran-C interface `_
#. `Interoperation of Fortran with C `_
#. `ISO_C_BINDING `_
#. `Iso_c_binding: Looking for practical example of how it helps with mangling `_
#. `Interoperability : Calling Fortran from C (i) `_
#. `Mixing C++ and Fortran `_
#. `Philip Semanchuk| Python, C, C++, and Fortran Relationship Status: It’s Not That Complicated `_
#. `Mixing C, C++, and Fortran `_
#. `Interoperable-Subroutines-and-Functions `_
#. `Interoperation of Fortran with C `_
#. `Iso_c_binding: Looking for practical example of how it helps with mangling `_
#. `Managing libraries (static and dynamic libraries) `_
#. `More on Calling C from FORTRAN `_
#. `Mixed C/Fortran Programming `_
#. `C-Fortran Interface `_
#. `C structs in Fortran `_
#. `Using Fortran from C/C++ `_
fortran2018-examples
-------------------------
#. `fortran2018-examples `_
#. `fortran-cpp-interface `_
#. `fortran-submodule `_
#. `Fortran MPI Examples `_
#. `Fortran Parallel Examples `_
#. `Sparse Fortran libraries `_
#. `Object-oriented Fortran NetCDF4 interface `_
#. `Geospace code `_
FORTRAN系列链接整理(FORTRAN series link)
--------------------------------------------
#. `FORTRAN系列链接整理 `_
#. `ubuntu22.04下查看gfortran版本号 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-Hello, World示例代码 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-Hello, World示例代码 version 1 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-Hello, World示例代码 version 2 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-addNumbers示例代码 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-testingInt示例代码 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-testingInt+integer+kind示例代码 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-division+Real Type示例代码 `_
#. `ubuntu22.04+gfortran11.4.0编译运行Fortran-variableTesting示例代码 `_
#. `ubuntu+gfortran+创建并使用subroutine+static lib简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+static lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+static lib简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+static lib v1简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(module)+static lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(module)+static lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+files)+static lib简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+files)+static lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+files)+static lib v2简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+files)+static lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+files)+static lib v1简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+files)+static lib v2简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+subroutines+files)+static lib简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+subroutines+files)+static lib v1简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+subroutines+files)+static lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+subroutines+files)+static lib v1 简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+static lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(single subroutine)+shared lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(single subroutine)+shared lib简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(single subroutine)+shared lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(single module)+shared lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(single module)+shared lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(single module)+shared lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(single module)+shared lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+files)+shared lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+files)+shared lib简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+files)+shared lib v1简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+files)+shared lib v1简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+subroutines+files)+shared lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+c调用Fortran+(multiple modules+subroutines+files)+shared lib v1 简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+subroutines+files)+shared lib简单测试 `_
#. `ubuntu+gcc+gfortran+c调用Fortran+(multiple modules+subroutines+files)+shared lib v1简单测试 `_
#. `windows11+oneAPI+icx+ifort+Fortran调用c+say_hello+static lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+Fortran调用c+say_hello+shared lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+Fortran调用c+print_string+static lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+Fortran调用c+print_string+shared lib 简单测试 `_
#. `ubuntu+gcc+gfortran+Fortran调用c+print_string+static lib 简单测试 `_
#. `ubuntu+gcc+gfortran+Fortran调用c+print_string+shared lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+Fortran调用c+c_add_integer+static lib 简单测试 `_
#. `windows11+oneAPI+icx+ifort+Fortran调用c+c_add_integer+shared lib 简单测试 `_
main.c
::
extern void show_N1();
extern void show_N2();
extern void show_N3();
int main(int argc, char *argv[])
{
show_N1();
show_N2();
show_N3();
return 0;
}
onemod.f90
::
module onemod
implicit none
integer, parameter :: N = 1024
contains
subroutine show_N() bind(C, name='show_N')
print*, "N = ", N
end subroutine show_N
end module onemod
Ubuntu+gcc+gfortran
::
$ gfortran -c onemod.f90
$ gfortran -c twomods.f90
$ ar r mods.a onemod.o twomods.o
------------------------------------------
check mods.a
------------------------------------------
$ nm mods.a
------------------------------------------
test(fortran)
$ gfortran -c main.f90 -I../
$ gfortran -o testprj main.o ../mods.a
------------------------------------------
test(c)
$ gcc -c main.c
$ gcc -o testprj main.o ../mods.a -lgfortran
Windows11+oneAPI+icx+ifort
::
$ cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'
$ ifort -c onemod.f90
$ lib /OUT:onemod.lib onemod.obj
----------------------------------
check onemod.lib
------------------------------------------
$ dumpbin /symbols onemod.lib
------------------------------------------
test(fortran)
$ ifort -c main.f90
$ ifort -o testprj main.obj ../onemod.lib
-------------------------------------
test(c)
$ icx -c main.c
$ icx -o testprj main.obj ../onemod.lib
Dumpbin
::
PS D:\work\fortran_work\ModernFortran\codes\windows\c-call-fortran-lib\static\01> dumpbin /symbols sub.lib
Microsoft (R) COFF/PE Dumper Version 14.37.32825.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file sub.lib
File Type: LIBRARY
COFF SYMBOL TABLE
000 00000000 SECT1 notype Static | .text
Section length 50, #relocs 3, #linenums 0, checksum 0
002 00000000 SECT1 notype () External | sub_
003 00000000 SECT2 notype Static | .rdata
Section length 10, #relocs 0, #linenums 0, checksum 0
005 00000008 SECT2 notype Static | __STRLITPACK_1
006 00000000 UNDEF notype () External | for_write_seq_lis
007 00000000 SECT3 notype Static | .xdata
Section length 8, #relocs 0, #linenums 0, checksum 0
009 00000000 SECT4 notype Static | .pdata
Section length C, #relocs 3, #linenums 0, checksum 0
00B 00000000 SECT2 notype Static | __STRLITPACK_3.0.1
00C 00000000 UNDEF notype () External | __ImageBase
00D 00000000 SECT5 notype Static | .drectve
Section length B9, #relocs 0, #linenums 0, checksum 0
String Table Size = 0x44 bytes
Summary
B9 .drectve
C .pdata
10 .rdata
50 .text
8 .xdata
Example1
--------------------------------------------
main.c
::
extern void hello_print();
int main( int argc, char**argv )
{
hello_print();
return 0;
}
sub.c
::
#include
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
$
)
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 = $
)
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
$
)
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 = $
)
::
cmake ..
cmake --build . --target print
cmake --build . --config Debug --target print
cmake --build . --config Release --target print
Example2
--------------------------------------------
sub.c
::
#include
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_link_libraries( FPrj
PRIVATE
$
)
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 = $
)
::
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"