Page 1 of 1

linking to a shared library

Posted: Sat Feb 13, 2010 3:14 pm
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.

Re: linking to a shared library

Posted: Sat Feb 13, 2010 8:42 pm
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 !

Re: linking to a shared library

Posted: Sun Feb 14, 2010 1:41 am
by skreamz
thank you. very much. :D