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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11372872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:44:10  来源:igfitidea点击:

What is the role of libc(glibc) in our linux app?

clinuxgnu

提问by basketballnewbie

When we debug a program using gdb, we usually see functions with strange names defined in libc(glibc?). My questions are:

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

  1. Is libc/glibcthe standard implementation of some standard C/C++ functions like strcpy,strlen,malloc?
  2. Or, is it not only of the first usage as described above, but also an wrapper of Unix/Linux system calls like open,close,fctl? If so, why can't we issue syscalls directly, without libc?
  3. Does libconly consist of one lib (.aor .so) file, or many lib files (in this case, libcis the general name of this set of libs)? Where do these lib file(s) reside?
  4. What is the difference between libcand 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什么区别?

回答by paulsm4

It's the "standard library". It's exactly like "MSVCRTL" in the Windows world.

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

The Gnu standard library ("glibc") is the implementation of libc most commonly (almost universally?) found on Linux systems. Here are the relevant files on an old SusE Linux system:

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

This link should answer any additional questions you might have (including references to the full and complete GLibc source code):

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

回答by duskwuff -inactive-

With regard to the first two, glibc is both the C standard library (e.g, "standard C functions") and a wrapper for system calls. You cannot issue system calls directly because the compiler doesn't know how -- glibc contains the "glue" which is necessary to issue system calls, which is written in assembly. (It is possible to reimplement this yourself, but it's far more trouble than it's worth.)

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

(The C++ standard library is a separate thing; it's called libstdc++.)

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

glibc isn't a single .so(dynamic library) file -- there are a bunch, but libc and libm are the most commonly-used two. All of the static and dynamic libraries are stored in /lib.

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

libc is a generic term used to refer to all C standard libraries -- there are several. glibc is the most commonly used one; others include eglibc, uclibc, and dietlibc.

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

回答by caf

libcimplements both standard C functions like strcpy()and POSIX functions (which may be system calls) like getpid(). Note that not all standard C functions are in libc- most math functions are in libm.

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

You cannot directly make system calls in the same way that you call normal functions because calls to the kernel aren't normal function calls, so they can't be resolved by the linker. Instead, architecture-specific assembly language thunks are used to call into the kernel - you can of course write these directly in your own program too, but you don't need to because libcprovides them for you.

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

Note that in Linux it is the combination of the kernel and libcthat provides the POSIX API. libcadds a decent amount of value - not every POSIX function is necessarily a system call, and for the ones that are, the kernel behaviour isn't always POSIX conforming.

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

libcis a single library file (both .soand .aversions are available) and in most cases resides in /usr/lib. However, the glibc (GNU libc) project provides more than just libc- it also provides the libmmentioned earlier, and other core libraries like libpthread. So libcis just one of the libraries provided by glibc - and there are other alternate implementations of libcother than glibc.

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

回答by Turgay Kale

You can check the detailed information about "libc" and "glibc" from the man pages on your linux sytem by typing "man libc" on the shell, copied as below;

您可以通过在 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.