Linux下將可執行文件和so一起發佈的技巧

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!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章