Skip to content
abandoftPublic

About

A modern, high-performance Fortran-to-C17 transpiler designed to generate portable, production-grade C code.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

F2C

English · 中文文档

A modern, high-performance Fortran-to-C17 transpiler that generates portable, production-grade C code.

Installation

Download a package for your platform from Releases, extract it, and add the extracted bin directory to your PATH. The commands below assume f2c is available on your PATH (f2c.exe on Windows).

Build from source

Requires Git, CMake 3.20 or newer, and a C17 compiler such as GCC, Clang, or MSVC. A Fortran compiler is not required.

git clone https://github.com/abandoft/f2c.git
cd f2c
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DF2C_BUILD_TESTING=OFF
cmake --build build --config Release --target f2c --parallel

With a single-configuration generator, the executable is build/f2c (build/f2c.exe on Windows). With Visual Studio, it is build/Release/f2c.exe. Add the directory containing the executable to your PATH, or invoke it by its full path.

Quick start

Save the following as hello.f90:

program hello
  implicit none
  print *, "Hello from Fortran"
end program hello

Translate, compile, and run it on Linux or macOS:

f2c hello.f90 -o hello.c
cc -std=c17 -O2 hello.c -lm -o hello
./hello

On Windows, translate with the same f2c command, then compile and run from an MSVC developer command prompt:

cl /std:c17 /O2 hello.c /Fehello.exe
hello.exe

F2C generates C source; a C compiler builds the executable. If your Fortran program calls external libraries, you must also provide and link their corresponding implementations.

Multiple source files

Pass related sources together, including the source for modules used by the program:

f2c math_module.f90 main.f90 -o app.c --header app.h

This produces one C source file and a header declaring the project's external procedures. Compile app.c as in the quick start. Use --header when you also need to call these procedures from C.

Command-line options

f2c [options] INPUT...
Option Description
-o FILE Write C to a file; defaults to standard output.
--header FILE Also write a C interface header.
--free-form, --fixed-form Set the source form for all inputs.
-I PATH Add an include search directory.
-DNAME[=EXPR] Define a preprocessing macro; the default value is 1.
-UNAME Remove a previously supplied macro definition.
--comments Preserve source lines as C comments.
--external-module NAME Permit a non-intrinsic module provided outside this translation.
--version Print the version.
-h, --help Show command-line help.

Source form is detected automatically: .f, .for, and .ftn use fixed form; extensions such as .f90 use free form. Use an override when the extension does not match the source layout. Preprocessing macro names are case-sensitive.

For example, add an include directory and enable a conditional source branch:

f2c -I include -DUSE_ISNAN=1 source.F90 -o output.c

Use - to read standard input or write standard output:

f2c --free-form - -o - < hello.f90 > hello.c

Supported Fortran

Implemented features include modules and procedures, numeric and character types, complex arithmetic, arrays and array operations, derived types, allocatable objects, pointers, and formatted and unformatted I/O. Both free-form and fixed-form sources are supported.

Fortran standard coverage is incomplete. When migrating a project, check the generated program against your existing tests; some features or combinations may require source changes.

Embedding

To use the translator from a C or C++ application, build from source and link the f2c::core CMake target. The public header provides f2c_transpile for one source buffer and f2c_transpile_project for multiple sources. Release results with f2c_result_free. Download packages contain the CLI and headers, but do not include a prebuilt library.

Feedback and releases

Report problems through Issues, including the F2C version, platform, command, a minimal Fortran example, and the expected and actual results. See CHANGELOG.md for version history. Contributions are welcome.

License

F2C is licensed under the MIT License. The archived netlib-f2c sources retain their original license; F2C does not depend on them.

About

A modern, high-performance Fortran-to-C17 transpiler designed to generate portable, production-grade C code.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages