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

时间:2012-07-07 06:37:48

标签: c linux gnu

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

  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 个答案:

答案 0 :(得分:55)

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的其他替代实现。

答案 1 :(得分:16)

关于前两个,glibc既是C标准库(例如“标准C函数”),也是系统调用的包装器。你不能直接发出系统调用,因为编译器不知道如何 - glibc包含发出系统调用所必需的“粘合剂”,这是用汇编语言编写的。 (有可能自己重新实现,但它比它的价值更麻烦。)

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

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

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

答案 2 :(得分:3)

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

答案 3 :(得分:1)

您可以查看有关" libc"的详细信息。和" glibc"通过键入" man libc"来自linux系统上的手册页在shell上,复制如下;

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.