Author Topic: Shared Libraries in Linux...  (Read 4852 times)

Offline EntityReborn

  • Single posting newcomer
  • *
  • Posts: 7
Shared Libraries in Linux...
« on: April 03, 2009, 05:22:20 am »
...are giving me a headache.

I have an app, that uses a library, lets call it libMyLib.so.
I have the files for libMyLib in a seperate directory:

Main Solution Directory
|
+-bin
|
+-MyApp
|
+-MyLib

'bin' is where all my targets get placed.
I include it in my libraries I need to link, using "../bin/Debug/libMyLib.so".
I compile it, and it has no problem, but when I run it from the bin/Debug directory in Terminal, I get the following:
Quote
./MyApp: error while loading shared libraries: ../bin/Debug/libMyLib.so: cannot open shared object file: No such file or directory

Notice that this error is looking for a bin/bin/Debug/libMyLib.so (extra bin intended) as '.' refers to bin/Debug at the time of running.
This is making me think that the location of the library is hardcoded. Why is that?

LD_DEBUG=libs offers no help, except for listing other (successful) loading libraries (libdl.so, in my current situation).
Speaking of libdl.so, THAT is giving me headaches too, as I ended up needing to give a relative path to including it. (I could not just add "libdl.so" to the librares to link, as it gave me the error "could not find -ldl.so" when trying to compile)

... BAH!
« Last Edit: April 03, 2009, 05:33:12 am by EntityReborn »

Offline grischka

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Shared Libraries in Linux...
« Reply #1 on: April 03, 2009, 09:47:32 am »
Well, first you need to get rid of the hardcoded path prefix, so you would replace
"../bin/Debug/libMyLib.so"
by
"-lMyLib -L../bin/Debug"

Then you need to tell the linker to put a library search path into MyApp, for example (please edit):
"-Wl,-rpath=/home/you/stuff/bin/Debug"

So your link command could look like this:
$ gcc <objects ...> -lMyLib -L../bin/Debug -Wl,-rpath=/home/you/stuff/bin/Debug -o ../bin/Debug/MyApp

Just in case ldd shows what MyApp is supposed to load.
$ ldd MyApp

I'd test this from a terminal first and then teach CodeBlocks to do something to the same effect, possibly with "Full Commandline Logging" turned on.

Offline EntityReborn

  • Single posting newcomer
  • *
  • Posts: 7
Re: Shared Libraries in Linux...
« Reply #2 on: April 03, 2009, 05:41:08 pm »
Thanks for the informative reply! I'll test this when I get back from work.
As I've never compiled from the Terminal, this could be interesting, so could you possibly point out places in C::B where I could implement this? (ie does "-lMyLib ...." go into the library linker commands area or the library list?)
Never seen the Full Commandline Logging option, mind pointing that out?

Thankz!
« Last Edit: April 03, 2009, 05:43:37 pm by EntityReborn »

Offline grischka

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Shared Libraries in Linux...
« Reply #3 on: April 04, 2009, 04:21:46 pm »
(ie does "-lMyLib ...." go into the library linker commands area or the library list?)
Maybe to the libraries without the "-l" or to the linker options including the "-l".  The GNU linker is somehow amnesic with what it sees on the commmandline so the order might matter.

Never seen the Full Commandline Logging option, mind pointing that out?
Mind looking yourself? 8) (Maybe under "Compiler Settings")

Offline EntityReborn

  • Single posting newcomer
  • *
  • Posts: 7
Re: Shared Libraries in Linux...
« Reply #4 on: April 24, 2009, 01:40:00 am »
OK, sorry for the wait, life got in the way. OK, so here is my current output:
Quote
-------------- Build: Debug in SDK ---------------

g++  -Wall -g -DBUILD_DLL     -c /home/EntityReborn/Desktop/Snippitz/Snippitz.SDK/BoundarySafeVector.cpp -o obj/Debug/BoundarySafeVector.o
g++  -Wall -g -DBUILD_DLL     -c /home/EntityReborn/Desktop/Snippitz/Snippitz.SDK/SnippitzEntry.cpp -o obj/Debug/SnippitzEntry.o
g++ -shared  obj/Debug/BoundarySafeVector.o obj/Debug/SnippitzEntry.o   -o ../bin/Debug/SDKDebug.so 
Output size is 5.62 KB

-------------- Build: Debug in Core ---------------

g++  -Wall -g    -I../Snippitz.SDK  -c /home/EntityReborn/Desktop/Snippitz/Snippitz.Core/Callback.cpp -o obj/Debug/Callback.o
g++  -Wall -g    -I../Snippitz.SDK  -c /home/EntityReborn/Desktop/Snippitz/Snippitz.Core/OS_APIs.cpp -o obj/Debug/OS_APIs.o
g++  -Wall -g    -I../Snippitz.SDK  -c /home/EntityReborn/Desktop/Snippitz/Snippitz.Core/PluginManager.cpp -o obj/Debug/PluginManager.o
g++  -Wall -g    -I../Snippitz.SDK  -c /home/EntityReborn/Desktop/Snippitz/Snippitz.Core/Snippitz.Core.cpp -o obj/Debug/Snippitz.Core.o
g++ -shared -L../bin/Debug  obj/Debug/Callback.o obj/Debug/OS_APIs.o obj/Debug/PluginManager.o obj/Debug/Snippitz.Core.o   -o ../bin/Debug/CoreDebug.so  ../bin/Debug/SDKDebug.so
Output size is 187.32 KB

-------------- Build: Debug in Console ---------------

g++ -Wall -fexceptions  -g    -I../Snippitz.Core -I../Snippitz.SDK  -c /home/EntityReborn/Desktop/Snippitz/Snippitz.Console/Main.cpp -o obj/Debug/Main.o
g++ -L/usr/lib/ -L/home/EntityReborn/Desktop/Snippitz/bin/Debug/  -o ../bin/Debug/ConsoleDebug obj/Debug/Main.o    -lCoreDebug.so /usr/lib/libdl.so
/usr/bin/ld: cannot find -lCoreDebug.so
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 4 seconds)
1 errors, 0 warnings
I have tried so many different variations of paths, messing with -Wl, --rpath, etc and this is just getting ridiculous.
Is my setup a bad one? Granted I started this project in XP, so it was just logical to place shared libs in the same directory, but still. I can send the project if needed. Basically all I want to do is be able to compile my program using libraries in the output folder, and then when the program runs, it looks in . instead of wherever.
One small note, I MUST put /usr/lib/libdl.so, I can't put just libdl.so when linking that library. Is this correct? I thought programs/the linker looked in that directory by default. Adding /usr/lib to linker search paths doesn't seem to work, and I get "Can't find -ldl.so".
« Last Edit: April 24, 2009, 01:56:57 am by EntityReborn »