【问题标题】:What is the role of libc(glibc) in our linux app?libc(glibc) 在我们的 linux 应用程序中的作用是什么?
【发布时间】:2012-07-07 13:00:55
【问题描述】:

当我们使用gdb 调试程序时,我们通常会看到在libc(glibc?) 中定义的具有奇怪名称的函数。我的问题是:

  1. libc/glibc 是一些标准 C/C++ 函数(如 strcpy,strlen,malloc)的标准实现吗?
  2. 或者,它不仅是上面描述的第一个用法,还是像open、close、fctl这样的Unix/Linux系统调用的包装器?如果是这样,为什么我们不能直接发出系统调用,而不使用libc?
  3. libc 是仅包含一个 lib(.a 或 .so)文件,还是包含多个 lib 文件(在这种情况下,libc 是这组 lib 的通用名称)?这些 lib 文件位于何处?
  4. libc 和 glibc 有什么区别?

【问题讨论】:

标签: c linux gnu


【解决方案1】:

它是“标准库”。它就像 Windows 世界中的“MSVCRTL”。

Gnu 标准库(“glibc”)是 Linux 系统上最常见(几乎普遍?)的 libc 实现。以下是旧 Suse Linux 系统上的相关文件:

ls -l /lib =>
-rwxr-xr-x  1 root root 1383527 2005-06-14 08:36 libc.so.6

ls -l /usr/lib =>
-rw-r--r--  1 root root 2580354 2005-06-14 08:20 libc.a
-rw-r--r--  1 root root     204 2005-06-14 08:20 libc.so

此链接应回答您可能遇到的任何其他问题(包括对完整和完整 GLibc 源代码的引用):

【讨论】:

    【解决方案2】:

    关于前两个,glibc 既是 C 标准库(例如,“标准 C 函数”)又是系统调用的包装器。您不能直接发出系统调用,因为编译器不知道如何——glibc 包含发出系统调用所必需的“胶水”,它是用汇编语言编写的。 (可以自己重新实现,但麻烦多于其价值。)

    (C++ 标准库是一个独立的东西;它被称为libstdc++。)

    glibc 不是一个单一的.so(动态库)文件——有很多,但 libc 和 libm 是最常用的两个。所有的静态库和动态库都存储在/lib。

    libc 是一个通用术语,用于指代所有 C 标准库——有几个。 glibc 是最常用的一种;其他包括 eglibc、uclibc 和 Dietlibc。

    【讨论】:

      【解决方案3】:

      libc 实现了标准 C 函数,如 strcpy() 和 POSIX 函数(可能是系统调用),如 getpid()。请注意,并非所有标准 C 函数都在 libc 中 - 大多数数学函数都在 libm 中。

      您不能像调用普通函数一样直接进行系统调用,因为对内核的调用不是普通函数调用,因此链接器无法解析它们。相反,特定于体系结构的汇编语言 thunk 用于调用内核 - 当然,您也可以直接在自己的程序中编写这些,但您不需要,因为 libc 为您提供了它们。

      请注意,在 Linux 中,提供 POSIX API 的是内核和 libc 的组合。 libc 增加了相当多的价值 - 并非每个 POSIX 函数都必须是系统调用,对于那些,内核行为并不总是符合 POSIX。

      libc 是一个单一的库文件(.so 和.a 版本都可用)并且在大多数情况下驻留在/usr/lib 中。然而,glibc (GNU libc) 项目提供的不仅仅是libc——它还提供了前面提到的libm,以及其他核心库,如libpthread。所以libc 只是 glibc 提供的库之一 - 除了 glibc 之外,libc 还有其他替代实现。

      【讨论】:

        【解决方案4】:

        你可以在你的 linux 系统的手册页中通过在 shell 上输入“man libc”来查看关于“libc”和“glibc”的详细信息,复制如下;

        LIBC(7)      Linux Programmer's Manual      LIBC(7)   
        
        NAME
               libc - overview of standard C libraries on Linux
        
        DESCRIPTION
               The term "libc" is commonly used as a shorthand for the "standard C library", a library of standard functions that can be
               used by all C programs (and sometimes by programs in other languages).  Because of some history (see below), use  of  the
               term "libc" to refer to the standard C library is somewhat ambiguous on Linux.
        
           glibc
               By  far  the most widely used C library on Linux is the GNU C Library ⟨http://www.gnu.org/software/libc/⟩, often referred
               to as glibc.  This is the C library that is nowadays used in all major Linux distributions.  It is  also  the  C  library
               whose details are documented in the relevant pages of the man-pages project (primarily in Section 3 of the manual).  Doc‐
               umentation of glibc is also available in the glibc manual, available via the command info libc.  Release 1.0 of glibc was
               made in September 1992.  (There were earlier 0.x releases.)  The next major release of glibc was 2.0, at the beginning of
               1997.
        
               The pathname /lib/libc.so.6 (or something similar) is normally a symbolic link that points to the location of  the  glibc
               library,  and executing this pathname will cause glibc to display various information about the version installed on your
               system.
        
           Linux libc
               In the early to mid 1990s, there was for a while Linux libc, a fork of glibc 1.x created by  Linux  developers  who  felt
               that  glibc  development  at  the  time  was  not  sufficing for the needs of Linux.  Often, this library was referred to
               (ambiguously) as just "libc".  Linux libc released major versions 2, 3, 4, and 5 (as well as many minor versions of those
               releases).  For a while, Linux libc was the standard C library in many Linux distributions.
        
               However, notwithstanding the original motivations of the Linux libc effort, by the time glibc 2.0 was released (in 1997),
               it was clearly superior to Linux libc, and all major Linux distributions that had been using  Linux  libc  soon  switched
               back to glibc.  Since this switch occurred long ago, man-pages no longer takes care to document Linux libc details.  Nev‐
               ertheless, the history is visible in vestiges of information about Linux libc that remain in some manual pages,  in  par‐
               ticular, references to libc4 and libc5.
        

        【讨论】:

        • 那是手册页!我想要一个要求区分 libc 和 glibc 的人..我应该使用的那个以及如何使用...如果有时间参考执行...哪个最适合行业...
        猜你喜欢
        • 2013-06-23
        • 2019-05-31
        • 2017-09-01
        • 2014-10-10
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        相关资源
        最近更新 更多