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