Identifying which Linux system library contains a function

前端 未结 6 610
误落风尘
误落风尘 2020-12-20 14:30

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

相关标签:
6条回答
  • 2020-12-20 14: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 :-)

    0 讨论(0)
  • 2020-12-20 14:52

    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 讨论(0)
  • 2020-12-20 14:54

    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
    
    0 讨论(0)
  • 2020-12-20 14:58

    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$ 
    
    0 讨论(0)
  • 2020-12-20 15:02

    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]
    
    0 讨论(0)
  • 2020-12-20 15:05

    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);
    
    0 讨论(0)
提交回复
热议问题