Home  >  Article  >  Backend Development  >  How to Link C and Fortran Binaries with GCC?

How to Link C and Fortran Binaries with GCC?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 10:43:30145browse

How to Link C   and Fortran Binaries with GCC?

Linking C and Fortran Binaries via GCC

Connecting C and C or C and Fortran code is straightforward using gcc and its g and gfortran derivatives. However, linking C and Fortran procedures can become problematic.

The Problem

Compiling C and Fortran source code separately yields object files when using g and gfortran, but linking them results in errors due to missing libraries. Neither compiler recognizes the libraries required by the other.

The Solution

To link binaries that combine C and Fortran, you need to explicitly include the necessary libraries. For g , use the -lgfortran flag to add the standard Fortran libraries:

g++ main.o print_hi.o -o main -lgfortran

Alternatively, gfortran can be utilized with the -lstdc flag:

gfortran main.o print_hi.o -o main -lstdc++

This action ensures that the linker includes the relevant libraries, allowing the C and Fortran code to interact seamlessly.

The above is the detailed content of How to Link C and Fortran Binaries with GCC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn