开发者

Identifying which Linux system library contains a function

I am using a dev system where I have to specify the lib name when accessing a function inside it.

I've used functions like open() before, and somehow found out that they'r开发者_如何学运维e in libc.so.

Now I want to use lstat(), but it appears that this one is not in libc. Sadly, the man pages I looked at do not document the location of the functions.

So, two questions:

  1. Can someone tell which lib hosts lstat?
  2. How can I generally find this out? Other than using grep "name" on all files in the lib folder, I mean.


Build a simple testcase in C, compile it and run 'ldd -r' on it to check what libs are loaded. If you don't get lstat() in C then you have a problem on your dev env. Or this env dates back before the age of symlinks :-)


This is one way to do it:

tomislav@malik:~$ cd /usr/lib
tomislav@malik:/usr/lib$ grep "lstat()" *
Binary file libperl.so.5.10 matches
Binary file libperl.so.5.10.0 matches
tomislav@malik:/usr/lib$ 


When I cross-compile Windows applications on Linux, if I have an issue with linking I tend to use this script that I named mingw-findin. A similar script could be used for regular Linux compilation, just instead of using the mingw alternative, use regular nm and instead of looking in the cross-compile prefixed directory, look in /usr/lib. To use this script, I run

./mingw-findin NameOfFunction

Here's the code:

#!/bin/sh
liblist=` ls /usr/x86_64-w64-mingw32/lib `

for i in $liblist
do

if x86_64-w64-mingw32-nm /usr/x86_64-w64-mingw32/lib/$i | grep -q $1; then
        echo $i
        x86_64-w64-mingw32-nm /usr/x86_64-w64-mingw32/lib/$i | grep $1
fi

done


Try this:

$ cat ./foobar.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void)
{
    struct stat buf;
    return lstat(".", &buf);
}


$ LD_DEBUG=bindings ./foobar 2>&1   | grep stat
31000:  binding file ./foobar [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: \
normal symbol `__lxstat' [GLIBC_2.2.5]


From the manpage (man lstat):

LSTAT(P)

NAME
       lstat - get symbolic link status

SYNOPSIS
       #include <sys/stat.h>

       int lstat(const char *restrict path, struct stat *restrict buf);


lstat is in libc, and libc is linked in by default. You don't need to do anything to use lstat besides including the header file for it #include <sys/stat.h>

man pages usually state which library they are in.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜