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!
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!
本文探讨了Linux环境下动态链接库(SO文件)的加载方式,并介绍了一种相对路径指定方法,使得程序及其依赖库可以作为一个整体被自由移动。
2812

被折叠的 条评论
为什么被折叠?



