Dec 9, 2009

How to correctly link a shared libray in GCC

This is one of the fucking weird problem which I tried to solve for two hours.

First, you need to prepare the library, which consists of header files (*.h) and libtanintest.so.

Here is the thing: When you link the shared library, libtanintest.so, to your code, you need to set library's directory (use -L) and library name (use -l) (small L).

Here is when it gets weird, your library name must be chopped into tanintest (remove lib in the front and .so in the back.

Therefore, the command to link library is :

gcc main.c-o output_file -L/lib_dir -ltanintest


If you do not link it, you will get error "undefined reference to ...".
If you link it incorrectly, you will get "the library cannot be found".