How to link with shared libs in a subdir of the exe dir? (linux)

Hello,

Sorry for the slight off topic, but I want to include SDL, SDL_mixer and
SDL_image in the directory of my game and link with these libs instead
of the libs in /usr/lib/. This is under Linux PPC. I spent sevaral
hours searching, but haven’t found how to do this yet.

I have tried the -rpath ./libs thing when linking, but ld returns an
error message “./libs unrecognized file format” (IIRC).

There is also the $LD_LIBRARY_PATH thing, but I don’t like this solution
and AFAIK it requires an absolute path.

Then I tried linking with -L./libs -lSDL etc., but that doesn’t work and
the exe is linked with /usr/lib/libSDL-1.2.so etc. even though I have
copied libSDL.so in ./libs/ ! There’s something I don’t understand
here.

Please help! :wink:
Thanks,–
Olivier.
Please remove “.ARGL.invalid” from my email when replying.
Incoming HTML mails are automatically deleted.

Try using LD_PRELOAD="./libs/libSDL.so ./libs/libSDL_mixer.so ./libs/libSDL_image.so"

do “man ld.so” for more info on the LD_* env vars.

in a script to run the game, you might name the game something like game.exe (I know it’s not a win32 exe file!)
and then you can name the script “game”, and do a chmod a+x game

the script would be something like:
--------------snip--------------------
#!/bin/sh
EXE=./game.exe
cd "dirname \"$0\"
if [ ! -x “$EXE” ] ; then
FULLEXE=”which \"$EXE\"
cd “dirname \"$FULLEXE\"
fi
if [ ! -x “$EXE” ] ; then
echo “Cannot find where $EXE is located!” >&2
exit 1
fi
export LD_PRELOAD=”./libs/libSDL.so ./libs/libSDL_mixer.so ./libs/libSDL_image.so"
exec “$EXE” ${1:+”$@”}
--------------snip--------------------
and that’s it…
change the “game.exe” on line 2 to the real name of the binary.

this script works for “./game”, “/path/to/game”, or just “game” when it’s found by the shell through $PATH .

you can check out any release of unreal tournament for linux, as they use a startup script too.

-LIM-

Olivier Fabre wrote:> Hello,

Sorry for the slight off topic, but I want to include SDL, SDL_mixer and
SDL_image in the directory of my game and link with these libs instead
of the libs in /usr/lib/. This is under Linux PPC. I spent sevaral
hours searching, but haven’t found how to do this yet.

I have tried the -rpath ./libs thing when linking, but ld returns an
error message “./libs unrecognized file format” (IIRC).

There is also the $LD_LIBRARY_PATH thing, but I don’t like this solution
and AFAIK it requires an absolute path.

Then I tried linking with -L./libs -lSDL etc., but that doesn’t work and
the exe is linked with /usr/lib/libSDL-1.2.so etc. even though I have
copied libSDL.so in ./libs/ ! There’s something I don’t understand
here.

Please help! :wink:
Thanks,

Olivier Fabre wrote:

There is also the $LD_LIBRARY_PATH thing, but I don’t like this solution
and AFAIK it requires an absolute path.

LD_LIBRARY_PATH doesn’t require an absolute path, no, and afaik it’s the
best option you have. The main gotcha is that rpath overrides it.
Since libSDL.so has an rpath set by default, and sdl-config adds a rpath
as well, you’ll have to modify the SDL Makefiles to link SDL without the
rpath (there’s no configure option to disable it, unfortunately), link
and install it, modify the sdl-config script to remove the rpath, and
relink your app. It’s a little work to set up, but once you’ve done
that it’s straightforward to use. (I use this within a dedicated build
environment for DROD, works perfectly. Just make sure to include the
right libSDL.so).

  • Gerry