lgfortran not found

Open the way picture Open the way · Jun 4, 2012 · Viewed 12.2k times · Source

I am using Ubuntu 10.04 and trying to compile some code that uses gfortran. At some point Makefiles does:

-L. -lgfortran 

and I get the error

/usr/bin/ld: cannot find -lgfortran

although it is installed:

ldconfig -p  |  grep   fortran
    libgfortran.so.3 (libc6,x86-64) => /usr/lib/libgfortran.so.3

How can I fix it?

P.S: The Makefile:

## FLAGS

CC:= gcc
C++:= g++
CFLAGS:= -c -O -Dintel -g 
FC:= gfortran
FFLAGS:= -c -O -cpp -g
LD:= g++
LDFLAGS:= -O


WETTER_CGAL_FLAGS:=  -g


#WETTER-Data
WETTER_cgal: weather.cpp surface_alg.h $(WETTER_CGAL_OBJECTS) WATT_interface.h data.cpp
    $(C++) $(WETTER_CGAL_FLAGS) -c weather.cpp -frounding-math
    $(C++) -c data.cpp -frounding-math 
    $(LD) $(WETTER_CGAL_OBJECTS) weather.o data.o -o WETTER_cgal -L. -lgfortran -lgmp -lCGAL -frounding-math -fp-model

Answer

Hristo Iliev picture Hristo Iliev · Jun 4, 2012

Does by any chance your gfortran version differ from the version of your g++? Or maybe it is installed in a different location?

The -lname option (in this case name is gfortran) instructs the linker to search for a library file called libname.a in the library search path. If found and no static linking is enforced by the -[B]static option the linker will search once again for libname.so and link against it instead (if found). If libname.a is not found an error will be given despite the presence of libname.so.

There should be a libgfortran.a somewhere in your gfortran installation. Search for it with find and provide the path to g++ with -L/path/to/compiler/libs. If g++ is the same version as your gfortran the path to libgfortran.a will already be present in the library search path (since both C/C++ and Fortran static libraries reside in the same place). It will not be present if both compilers differ in their version though.

For example on a 64-bit RedHat based system libgfortran.a is located in /usr/lib/gcc/x86_64-redhat-linux/<GCC version>/ while the shared libgfortran.so.* are located in /usr/lib64.

An alternative solution is to replace -lgfortran with /usr/lib/libgfortran.so.3.

The -L. option is rather related to -lCGAL than to -lgfortran.