您的位置:首页 > 运维架构 > Linux

Linux下将可执行文件和so一起发布的技巧

2008-05-23 21:34 627 查看
From Chad Austin ( http://aegisknight.org/ )

One thing I've always liked about Windows is that if you ship an executable
and several DLLs, you just include the DLLs in the same directory as the EXE,
and the loader finds them when the program is loaded. However, the Linux
runtime loader does not search for SOs in the same way. The way most people
work around this (i.e. Mozilla) is by shipping a shell script called 'mozilla'
which sets up LD_LIBRARY_PATH -- enabling the loader to find the right SOs --
and then calling mozilla-bin. It works, but it's pretty hacky.

Well, today, I've found a much better way to do it... Apparently ld.so
expands $ORIGIN in rpath and such to be the location of the executable.
So here's how you specify an rpath relative to the executable:

gcc '-Wl,-rpath=$ORIGIN/bar' -o foo foo.c -Lbar -lbar

Now you can move foo and bar/libbar.so anywhere and they'll still run!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: