Identifying which Linux system library contains a function

血红的双手。 提交于 2019-11-29 13:21:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!