我们的Linux应用程序中libc(glibc)的作用是什么?

97

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

  1. libc/glibc 是一些标准 C/C++ 函数的标准实现吗,例如 strcpystrlenmalloc
  2. 或者,它不仅仅是上述第一个用途,而且还是 Unix/Linux 系统调用的封装,如 openclosefcntl?如果是这样,为什么不能直接发出系统调用,而不需要使用 libc
  3. libc 是否只由一个库文件 (.a 或者 .so) 组成,或者由许多库文件组成(在这种情况下,libc 是这个库集合的通用名称)?这些库文件位于哪里?
  4. libcglibc 之间有什么区别?

我在这里找到了一些有用的信息 - http://www.linux-m68k.org/faq/glibcinfo.html。 - Victor Yarema
4个回答

118

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 实现。


35
关于前两个方面,glibc既是C标准库(例如,“标准C函数”),也是系统调用的包装器。您无法直接发出系统调用,因为编译器不知道如何发出系统调用-- glibc包含必要的“粘合剂”,以便发出系统调用,这些粘合剂是用汇编语言编写的。(您可以自行重新实现它,但这比它的价值大得多。)
(C++标准库是另一回事;它被称为libstdc ++。)
glibc不是单个.so(动态库)文件--有许多这样的文件,但libc和libm是最常用的两个。所有静态和动态库都存储在/lib中。 libc是指所有C标准库的通用术语--有几种。glibc是最常用的一个;其他包括eglibc、uclibc和dietlibc。

6

这是“标准库”。它与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
您可以通过在shell中键入“man libc”来查看有关“libc”和“glibc”的详细信息,如下所示;请注意,此命令仅适用于Linux系统。
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.

1
那是一个手册页!我想了解请求libc和glibc之间的区别的人...我应该使用哪个...如果有时间参考执行...哪个对行业最好... - aurelien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接