8.3.3.2 Libraries

  • -llibrary

    The -llibrary option scans the library named library for unresolved symbols when linking. The linker searches a standard list of directories for the library with the name library.a. It makes a difference where you write this option in the command. The linker processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in libz.a, those functions may not be loaded.

    The directories searched include several standard system directories, plus those specified with -L.

    Usually, 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 filename (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.

  • -Ldir

    Add a directory to the list of directories to be searched for -L.