Home > Backend Development > C++ > How to Link Fortran and C Binaries Without Missing Library Dependencies?

How to Link Fortran and C Binaries Without Missing Library Dependencies?

Linda Hamilton
Release: 2024-10-26 03:28:28
Original
934 people have browsed it

 How to Link Fortran and C   Binaries Without Missing Library Dependencies?

Linking Fortran and C Binaries: Resolving Library Mismatches

When developing projects that involve both C and Fortran code, it becomes necessary to link the resulting binaries together to create a cohesive program. However, attempting to compile such projects using either g or gfortran alone can lead to errors due to missing library dependencies.

To resolve these issues, it is essential to use the appropriate linking flag that specifies the required library for the other language.

Solution for Linking C and Fortran Binaries

When linking a project involving C and Fortran, follow these steps:

  • Use g to compile the C source code: g -c main.cpp -o main.o
  • Use gfortran to compile the Fortran source code: gfortran -c print_hi.f90 -o print_hi.o
  • Link the compiled object files using g : g main.o print_hi.o -o main -lgfortran

This command will include the necessary Fortran libraries (-lgfortran) when linking, thereby resolving the undefined reference errors encountered with g alone.

Alternative Approach

If you prefer to use gfortran for linking, you can achieve the same result by passing the -lstdc flag:

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

This flag will incorporate the required C libraries (-lstdc ) during linking, addressing the undefined references faced when using gfortran exclusively.

By utilizing the appropriate linking flags, you can successfully combine C and Fortran binaries into a fully functional program.

The above is the detailed content of How to Link Fortran and C Binaries Without Missing Library Dependencies?. 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