When a C program is compiled, the compiler generates object code. After generating the object code, the compiler will invoke linker. One of the main tasks of the linker is to make code of library functions available to the program, which can be accomplished through two methods: using static libraries or using dynamic libraries.

Static library

Static libraries are .a (or in Windows .lib) files.

The static library is created and updated by the ar utility. The library file’s name is libxxx.a, starting with lib and .a as the suffix. Take ANSI C as an example, it defines a wide range of standard I/O and string operations, such as atoi, printf, strcpy, etc., which exist in the libc.a library, and floating-point functions in the libm.a library. Its disadvantage is that the program requires a copy of the objects needed in the static library when it is running. Therefore, it is not easy to maintain and update.

Shared library

Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.

Shared libraries and Dynamic libraries are the same.

Dynamic libraries are referenced by programs using it at run-time. Dynamic linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory.

Conventionally, the library file is named by libxxx.so, starting with lib and .so as the suffix. For example, to invoke the libraries such as thread and socket, you only need to add the compiler options -lthread and -lsocket respectively ( Note : always put the -lxxx option to the very right of the compile command parameters).

You can use ldd command to view the shared libraries needed by the current executable at run time. Dynamic libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared objects with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it.

Using a dynamic library will make the program dependent on the external environment. If the environment lacks a dynamic library or if the version of the library is incorrect, the program will not run (Linux: the lib not found).

-I -L -l


gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld
  • -I (Capital i)

    Set /home/hello/include as the first directory to search the header files. The searching order is: /home/hello/include —> /usr/include—> /usr/local/include .

  • -L (Capital l)

    Set /home/hello/lib as the first directory to search the library files. The searching order is: /home/hello/lib —> /lib—> /usr/lib —>/usr/local/lib .

  • -l (Lowercase l)

    Search dynamic library file libworld.so in the directory -L specified. (If “-static” option is used in the gcc command, it means the linker should search the static library file libworld.a).