Fortran
Fortran, as derived from Formula Translating System, is a general-purpose, imperative programming language. It is used for numeric and scientific computing.
LINK : fatal error LNK1104: cannot open file ‘ifmodintr.lib’
Parallel programming without MPI - Using coarrays in 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
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
Compiling a mixed C-Fortran program (main program is Fortran)
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\"
Fortran Formats
Mixed-Programming
fortran2018-examples
FORTRAN系列链接整理(FORTRAN series link)
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-testingInt+integer+kind示例代码
ubuntu22.04+gfortran11.4.0编译运行Fortran-division+Real Type示例代码
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 简单测试
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 简单测试
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 <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"