开发者

What is a reliable way to determine which shared library will be loaded across linux platforms?

开发者 https://www.devze.com 2022-12-21 12:10 出处:网络
I need to find out which library will be loaded given in the information returned from /sbin/ldconfig.I came up with the following:

I need to find out which library will be loaded given in the information returned from /sbin/ldconfig. I came up with the following:

#!/bin/bash
echo $(dirname $(/sbin/ldconfig -p | awk "/$1/ {print \$4}" | head -n 1))

Running this results with:

$ whichlib libGL.so
/usr/X11R6/lib

This a two part question:

  1. Will this produce a reliable result across platform?
  2. Is there a slicker way to parse the output开发者_StackOverflow of ldconfig?

Thanks, Paul


There're several ways the library is loaded by executeable: 1.

  1. Using $LD_LIBRARY_PATH
  2. Using ld cache
  3. Libary with full path compiled into binary (-rpath gcc flag)

You're using option 2, while option 1 and 3 are not considered.


Depending on what exactly you're doing you may want to run ldd directly on the executable you're planning to run rather than the general case ldconfig.

Since you asked, you could write your script like this:

dirname "$(/sbin/ldconfig -p | awk "\$1 == "$1" {print \$4; exit}")"

It's a little more precise and has one less pipe. Also echo $(cmd) is redundant; you can just write cmd.

0

精彩评论

暂无评论...
验证码 换一张
取 消