85

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

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

4 回答 4

98

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

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

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

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

于 2012-07-07T07:28:22.707 回答
27

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

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

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

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

于 2012-07-07T07:27:20.243 回答
4

它是“标准库”。它就像 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 源代码的引用):

于 2012-07-07T06:42:16.973 回答
-2

您可以通过在 shell 上键入“man libc”,从 linux 系统的手册页中查看有关“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.
于 2018-02-19T13:21:27.257 回答