2012-07-07 308 views
26

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

  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和有什么不一样?

回答

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的源代码参考):

+0

TKS,真诚,paulsm4 – basketballnewbie 2012-07-10 10:26:36

10

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

(C++标准库是一个独立的东西,这就是所谓的libstdc++

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

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

+0

duskwuff,你是很清楚的,享受你的help.really, – basketballnewbie 2012-07-10 10:27:29

37

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

+0

咖啡馆,只是由于你 – basketballnewbie 2012-07-10 10:26:53

0

您可以通过键入壳上“人的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. 
相关问题