本文介绍了什么的libc(glibc的)在我们的Linux应用中的作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们用gdb调试程序,我们通常看到的功能在libc中定义的奇怪的名字(glibc的?)。我的问题是:


  1. 是的libc / glibc的标准执行一些标准的C / C ++的功能,如strcpy的,strlen的,的malloc?

  2. 或者,是它不仅如上所述,也Unix / Linux系统的包装要求,如开放,关闭,FCTL第一次使用的?如果是这样,为什么我们不能直接发出的系统调用,没有libc的?

  3. libc中是否只由一个LIB(某或的.so)文件,或许多一个lib文件(在这种情况下,libc的是这套LIB的一般名称)?在哪里做这些LIB文件(县)所在?

  4. 是什么的libc和glibc有什么区别?


解决方案

的libc 实现像 strcpy的两个标准C函数()和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 的其它替代实现。

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

  1. Is libc/glibc the 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 libc only consist of one lib (.a or .so) file, or many a lib files (in this case, libc is the general name of this set of lib)? Where do these lib file(s) reside?
  4. What is the difference between libc and glibc?
解决方案

libc implements 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.

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 libc provides them for you.

Note that in Linux it is the combination of the kernel and libc that provides the POSIX API. libc adds 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.

libc is a single library file (both .so and .a versions 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 libm mentioned earlier, and other core libraries like libpthread. So libc is just one of the libraries provided by glibc - and there are other alternate implementations of libc other than glibc.

这篇关于什么的libc(glibc的)在我们的Linux应用中的作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:25