linking to a shared library

General questions regarding the usage of CodeLite
skreamz
CodeLite Curious
Posts: 4
Joined: Sat Nov 14, 2009 11:31 am
Genuine User: Yes
IDE Question: c++
Contact:

linking to a shared library

Post by skreamz »

hey all, sorry for the silly question but cant seem to figure it out.

im using linux, i have a workspace with 2 projects, 1 is a executable and the other is a shared library (not static).
i am trying to use the shared library in my executable i have included the headers from the shared library project but cant seem to figure out how to tell the executable project to use the shared library.


tnx for any help.
josee
CodeLite Enthusiast
Posts: 37
Joined: Fri Feb 05, 2010 10:57 am
Genuine User: Yes
IDE Question: C++
Contact:

Re: linking to a shared library

Post by josee »

Hi,

to use a shared library under Linux you have to instruct the Linker of your application to bind to the shared library.
Open application project "Settings..." "Linker" Tab and add something like the following to "Options:"

-L$(IntermediateDirectory) -l<nameoflib> -Wl,-rpath,$(IntermediateDirectory)

I assume that $(IntermediateDirectory) is pointing to the directory where your app and also shared library is stored.
Your shared library should by named like "lib<nameoflib>.so". And do not forget to compile the shared lib with "-fPIC"
and "-shared" (both Compiler+Linker).

PS:
-L sets the search path for the linker
-l trys to link against a library named lib<nameoflib>.so (shared version) or lib<nameoflib>.a (static version)
-Wl, tells gcc to hand the follwoing over to the linker
-Wl,-rpath,<path> hands the option "-rpath <path>" to the linker which tells it to search for shared libraries in the directory <path>
at application start up. <path> should be relative to your working directory !
skreamz
CodeLite Curious
Posts: 4
Joined: Sat Nov 14, 2009 11:31 am
Genuine User: Yes
IDE Question: c++
Contact:

Re: linking to a shared library

Post by skreamz »

thank you. very much. :D
Post Reply