Search code examples
c++gccld

GCC: -lxxx vs (libxxx.a or libxxx.so)


Is there some differences between -lxxx and libxxx.a or libxxx.so when compiling using GCC?

From the book CSAPP Chapter 7: Linking, it said:

The -lvector argument is a shorthand for libvector.a

So the -lxxx is only a shorthand for libxxx.a or libxxx.so?


Solution

  • Lets say you have a file /lib/libxxx.a and you want to link it to foo.o. Then gcc -L /lib -lxxx foo.o is equivalent to gcc foo.o /lib/libxxx.a. -L flag specifies your library search path. The difference between .a(archive) and .so(shared object) is static library vs shared library. Simply put, a static library is linked to your program at compilation time whereas a shared library is only loaded to memory at running time.