Calling C Math Functions from Fortran Using ISO_C_BINDING
Learn how to safely call a C math routine such as sin from Fortran using ISO_C_BINDING, with compile, link, and verification steps.
08 Mar 2026, 14:14 UTC

Desired outcome
\nInvoke a C library routine (e.g., the sin function from libm) from a Fortran program and obtain the same result as Fortran’s intrinsic SIN.
Prerequisites
\n- \n
- A Fortran 2003‑or‑later compiler that provides the intrinsic module
ISO_C_BINDING(e.g., gfortran ≥ 4.6, ifort, nagfor). \n - A C compiler and the standard math library (
libmon Linux/macOS,msvcrton Windows). \n - Access to a terminal or command prompt with permission to compile and link executables. \n
Procedure
\n- \n
Create a Fortran source file, e.g.,
\ncall_csin.f90, that usesISO_C_BINDINGto declare the C function.
\nprogram call_csin\n use, intrinsic :: iso_c_binding\n implicit none\n real(c_double) :: x, y_c, y_f\n interface\n real(c_double) function c_sin(x) bind(c, name='sin')\n import :: c_double\n real(c_double), value :: x\n end function c_sin\n end interface\n\n x = 0.5_c_double\n y_c = c_sin(x) ! call the C sin\n y_f = sin(x) ! Fortran intrinsic for comparison\n print *, 'C sin(', x, ') = ', y_c\n print *, 'Fortran sin(', x, ') = ', y_f\n print *, 'Difference = ', abs(y_c - y_f)\nend program call_csin\n\nCompile the Fortran source to an object file. Run the command in a terminal where you have write permission for the current directory.
\n
\ngfortran -c -Wall -fcheck=all call_csin.f90 -o call_csin.oReplace
\ngfortranwith your Fortran compiler if needed. The flags-Walland-fcheck=allhelp catch mismatches early. \nCompile a trivial C wrapper (optional) or directly link the Fortran object with the math library. The C function
\nsinresides inlibm, so we link with-lm.
\ngfortran call_csin.o -o call_csin -lmOn Windows with MSVC, you would link against
\nlegacy_stdio_def.libandvcruntime.lib; the principle is the same. \nRun the resulting executable.
\n
\n./call_csin\n
Expected checks
\n- \n
- Compilation completes without warnings about type mismatches or missing
BIND(C)attributes. \n - Linking succeeds; the linker does not report undefined references to
sin. \n - Execution prints two nearly identical numbers and a difference close to machine epsilon (e.g.,
1.000000000000000E-016or smaller). \n - No segmentation fault or runtime error occurs. \n
Recovery options
\n- \n
- Linking fails with “undefined reference to `sin’”: ensure you added
-lm(or the platform‑specific math library) after the object file. Verify spelling and case; on some systems the symbol may be prefixed with an underscore. \n - Wrong numeric results: check that the Fortran kind matches the C type. Use
integer(c_int),real(c_double), etc., as shown. Avoid defaultintegerorrealunless you have confirmed they are interchangeable with the C types on your platform. \n - Suspected name‑mangling issues: inspect the generated symbol with
nm call_csin.oorobjdump -t call_csin.o. The Fortran compiler may append an underscore; adjust thename=''argument in thebind(c, name='…')clause accordingly. \n - Debugging: recompile with
-gand usegdbto step into the C function; verify that the argument value received by C matches the Fortran input. \n
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.