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:
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!