Ordering object files and archives on the linker command line is very important on Unix systems, since the default behavior of the linker is to search for symbol definitions only in archives following the object file or archive where an unresolved link related to a single-pass link was found.
This means that if your code refers to __netcdf_MOD_nf90_strerror , then the archive containing the definition of this symbol ( libnetcdff.a ) should appear after the list of object files from your program. libnetcdff.a itself refers to characters from the C library libnetcdf.a , so it must be linked after libnetcdff.a . Therefore, the correct order of links:
/tmp/ccE6g7sr.o libnetcdff.a libnetcdf.a
where /tmp/ccE6g7sr.o is the temporary file of the object that the assembler creates from the compiled source file. The correct command line to compile your code:
f95 -o xy -I/usr/include/ simple_xy_wr.f90 -lnetcdff -lnetcdf
In this case, the linker is not called directly, but rather the compiler does it. GCC compilers pass all the binding-related things in the same order to an intermediate utility called collect2 , which then calls the actual linker ld .
Please note that if there are public versions of netCDF library libraries (i.e. there are libnetcdff.so and libnetcdf.so ), then the linker will prefer them to static archives (if static linking is not enabled using -static ), and the final link phase will be processed Runtime Link Editor (RTLD) ( /lib64/ld-linux-x86-64.so.2 on Ubuntu). In this case, the same command line as in your question will be really successful without link errors, despite the fact that both libraries are located in front of the code that refers to them, since missing links to characters will be resolved by RTLD when loading the executable file file.
source share