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

How to Link C and Fortran Binaries with GCC?

Mary-Kate Olsen
Release: 2024-10-26 10:43:30
Original
261 people have browsed it

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
Copy after login

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

gfortran main.o print_hi.o -o main -lstdc++
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template