您的意思是在程序启动时使用的绝对路径并且在查找库时受到青睐?rpath
正是这样。它将覆盖在 LD_LIBRARY_PATH 中设置的默认搜索路径和内容。只需告诉 gcc 将其传递给链接器:
g++ -Wl,-rpath,/usr/lib/my_4.1 -omy_binary *.cpp
您可以让它向您显示搜索处理(使用help
它为您提供更多选择):
[js@HOST2 cpp]$ LD_DEBUG=libs ./a.out
5859: find library=libc.so.6 [0]; searching
5859: search path=/usr/lib/my_4.1/tls/i686/sse2:/usr/lib/my_4.1/tls/i686:
/usr/lib/my_4.1/tls/sse2:/usr/lib/my_4.1/tls:
/usr/lib/my_4.1/i686/sse2:/usr/lib/my_4.1/i686:
/usr/lib/my_4.1/sse2:/usr/lib/my_4.1 (RPATH from file ./a.out)
5859: trying file=/usr/lib/my_4.1/tls/i686/sse2/libc.so.6
5859: ....
5859: search cache=/etc/ld.so.cache
5859: trying file=/lib/libc.so.6 (note: found here!)
5859: