This page explains how to build programs containing both Fortran and C/C++ source code on OCTOPUS. Since runtime environments and standard libraries differ by language, you must specify the correct standard libraries when linking mixed-language programs.
Note that this guide uses the Intel oneAPI DPC++/C++ Compiler (icx/icpx) and Intel Fortran Compiler (ifx) included in the BaseCPU module.
Calling a C Function from a Fortran Main Program
Assume the main program is defined in "main.f90" and the function "hello" is defined in "hello.c" as follows.
|
1 2 3 4 5 6 7 8 |
program main implicit none interface subroutine hello() bind(c) end subroutine end interface call hello end program |
|
1 2 3 4 5 6 |
#include <stdio.h> void hello() { printf("hello, world\n"); } |
After compiling the source code with each language's compiler, link the objects with the Fortran compiler to generate an executable file.
ifx -c main.f90 # Compile Fortran source code
icx -c hello.c # Compile C source code
ifx -o hello main.o hello.o # Link objects
You can also link objects with the C compiler. In that case, use the -fortlib option to link the Fortran standard library.
icx -o hello -fortlib main.o hello.o # Link objects
Calling a C++ Function from a Fortran Main Program
Assume the main program is defined in "main.f90" and the function "hello" is defined in "hello.cpp" as follows.
|
1 2 3 4 5 6 7 8 |
program main implicit none interface subroutine hello() bind(c) end subroutine end interface call hello end program |
|
1 2 3 4 5 6 |
#include <iostream> extern "C" void hello() { std::cout << "hello, world" << std::endl; } |
After compiling the source code with each language's compiler, link the objects with the Fortran compiler to generate an executable file. At that time, link the C++ standard library using the -cxxlib option.
ifx -c main.f90 # Compile Fortran source code
icpx -c hello.cpp # Compile C++ source code
ifx -o hello -cxxlib main.o hello.o # Link objects
You can also link objects with the C++ compiler. In that case, use the -fortlib option to link the Fortran standard library.
icpx -o hello -fortlib main.o hello.o # Link objects
Calling a Fortran Subroutine from a C Main Function
Assume the main function is defined in "main.c" and the subroutine "hello" is defined in "hello.f90" as follows.
|
1 2 3 4 5 6 7 8 |
extern void hello_(); int main() { hello_(); return 0; } |
|
1 2 3 4 5 |
subroutine hello implicit none print *,'hello, world' end subroutine hello |
After compiling the source code with each language's compiler, link the objects with the C compiler to generate an executable file. At that time, link the Fortran standard library using the -fortlib option.
icx -c main.c # Compile C source code
ifx -c hello.f90 # Compile Fortran source code
icx -o hello -fortlib main.o hello.o # Link objects
You can also link objects with the Fortran compiler. In that case, use the -nofor-main option to specify that the main program is not Fortran.
ifx -o hello -nofor-main main.o hello.o # Link objects

