Linking a shared library

Marc Glisse
Sat Jun 2 05:54:00 GMT 2018

On Fri, 1 Jun 2018, Edward Diener wrote:

> An executable I am creating needs to link and run with a shared library 
> called, let's say, '' in a relative directory called, let's say 
> 'mylibs/lib'.
> Does using the -L option while compiling the source files to object files add 
> the relative directory 'mylibs/lib' to the library search path for shared 
> libraries when linking ?

No. Since -L tells -l where to look, why would you want to separate them 
into 2 separate commands? Did some documentation recommend that somewhere?

clang even gives you a warning if you pass -L to a compile (-c) command.

> Do I specify the shared library to the linker as '-lmyshare' when linking or 
> do I rather specify the shared library simply as '' when linking 
> ?

-lmyshare (or you can pass /full/path/to/ and omit the -L 

> I have tried using '-Lmylibs/lib' when compiling and '-lmyshare' when linking 
> but the linker fails with the message:
> /usr/bin/ld: cannot find -lmyshare
> so I am doing something wrong and do not know what it is.

(your next question will likely be how to tell where to find the library 
at runtime...)

Marc Glisse

More information about the Gcc-help mailing list