5.7.10.2 L: Specify Library File Option

The -llibrary option scans the named library file for unresolved symbols when linking.

When this option is used, the linker will search a standard list of directories for the library with the name library.a. The directories searched include the standard system directories, plus any that you specify with the -L option.

The linker processes libraries and object files in the order they are specified, so it makes a difference where you place this option in the command. The options (and in this order), foo.o -llibz bar.o search library libz.a after file foo.o but before bar.o. If bar.o refers to functions in libz.a, those functions may not be loaded.

Normally the files found this way are library files (archive files whose members are object files). The linker handles an archive file by scanning through it for members which define symbols that have been referenced but not defined yet. But if the file found is an ordinary object file, it is linked in the usual fashion.

The only difference between using an -l option (e.g., -lmylib) and specifying a file name (e.g., mylib.a) is that the compiler will search for a library specified using -l in several directories, as specified by the -L option.

By default the linker is directed to search <install-path>/lib for libraries specified with the -l option. This behavior can be overridden using environment variables.

See also the INPUT and OPTIONAL linker script directives.