02.20 libc、glibc和glib的关系

在进行Linux开发不可避免的要调用一些系统的API,我们经常开到的概念有诸如glibc、libc和glib等等。那么这些系统库之前的关系是什么样的呢?下面小编带大家了解一下这些库之前的共同点和差异,希望对以后的编程开发提供帮助。

Linux开发必知内容整理 | libc、glibc和glib的关系

glibc 和 libc的关系

glibc 和 libc 都是 Linux 下的C函数库。libc是Linux下的ANSI C函数库;glibc 是 Linux 下的 GUN C函数库。

那么ANSI C和GNU C有什么区别呢?

ANSI C函数库是基本的C语言函数库,包含了C语言最基本的库函数。这个库可以根据头文件划分为 15 个部分,其中包括:

  1. <ctype.h>:包含用来测试某个特征字符的函数的函数原型,以及用来转换大小写字母的函数原型;/<ctype.h>
  2. <errno.h>:定义用来报告错误条件的宏;/<errno.h>
  3. <float.h>:包含系统的浮点数大小限制;/<float.h>
  4. <math.h>:包含数学库函数的函数原型;/<math.h>
  5. <stddef.h>:包含执行某些计算 C 所用的常见的函数定义;/<stddef.h>
  6. <stdio.h>:包含标准输入输出库函数的函数原型,以及他们所用的信息;/<stdio.h>
  7. <stdlib.h>:包含数字转换到文本,以及文本转换到数字的函数原型,还有内存分配、随机数字以及其他实用函数的函数原型;/<stdlib.h>
  8. <string.h>:包含字符串处理函数的函数原型;/<string.h>
  9. <time.h>:包含时间和日期操作的函数原型和类型;/<time.h>
  10. <stdarg.h>:包含函数原型和宏,用于处理未知数值和类型的函数的参数列表;/<stdarg.h>
  11. <signal.h>:包含函数原型和宏,用于处理程序执行期间可能出现的各种条件;/<signal.h>
  12. <setjmp.h>:包含可以绕过一般函数调用并返回序列的函数的原型,即非局部跳转;/<setjmp.h>
  13. <locale.h>:包含函数原型和其他信息,使程序可以针对所运行的地区进行修改。/<locale.h>
  14. 地区的表示方法可以使计算机系统处理不同的数据表达约定,如全世界的日期、时间、美元数和大数字;
  15. <assert.h>:包含宏和信息,用于进行诊断,帮助程序调试。/<assert.h>

GNU C函数库是一种类似于第三方插件的东西。由于 Linux 是用C语言写的,所以Linux的一些操作是用C语言实现的。因此,GUN 组织开发了一个 C 语言的库以便让我们更好的利用 C 语言开发基于 Linux 操作系统的程序。不过现在的不同的 Linux 的发行版本对这两个函数库有不同的处理方法,有的可能已经集成在同一个库里了。

查看当前系统的 glibc 版本的方法:

通过执行libc.so.6

切换到libc.so.6所在的目录,执行如下命令。

[root@itworld123]# ./libc.so.6
GNU C Library (GNU libc) stable release version 2.28.
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 8.2.1 20180905 (Red Hat 8.2.1-3).
libc ABIs: UNIQUE IFUNC ABSOLUTE
For bug reporting instructions, please see:
<http>.
/<http>

执行ldd命令

[root@itworld123]# ldd --version
ldd (GNU libc) 2.12
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
由 Roland McGrath 和 Ulrich Drepper 编写。

glibc 和 glib的关系

错误观点:GLib前面有个"g",所以认为glib是GNU的东东;同时认为glibc是GLib 的一个子集。其实,glib和glibc 基本上没有太大联系,可能唯一的共同点就是,其都是C编程需要调用的库而已。

GLib是Gtk+库和Gnome的基础。GLib是一个跨平台的、用C语言编写的五个底层库的集合,为GNOME所使用。其支持的平台包括Linux、Unix和Windows 等。GLib为许多标准的、常用的C语言结构提供了相应的替代物。GLib中包含了近二十种实用功能,从简单的字符处理到初学者很难理解的XML解析功能。

官方说明

关于Glib的官方说明:

GLib is a general-purpose utility library, which provides many useful data types, macros, type conversions, string utilities, file utilities, a main loop abstraction, and so on. It works on many UNIX-like platforms, Windows, OS/2 and BeOS. GLib is released under the GNU Library General Public License (GNU LGPL).

The general policy of GLib is that all functions are invisibly threadsafe with the exception of data structure manipulation functions, where, if you have two threads manipulating the same data structure, they must use a lock to synchronize their operation.

GLib is the low-level core library that forms the basis for projects such as GTK+ and GNOME. It provides data structure handling for C, portability wrappers, and interfaces for such runtime functionality as an event loop, threads, dynamic loading, and an object system.

关于Glibc的官方说明

Overview:

Any Unix-like operating system needs a C library: the library which defines the ``system calls'' and other basic facilities such as open, malloc, printf, exit...

The GNU C Library is used as the C library in the GNU systems and most systems with the Linux kernel.

Project Goals:

The GNU C Library is primarily designed to be a portable and high performance C library. It follows all relevant standards including ISO C11 and POSIX.1-2008. It is also internationalized and has one of the most complete internationalization interfaces known.

History:

The history of Unix and various standards determine much of the interface of the C library. In general the GNU C Library supports the ISO C and POSIX standards. We also try to support the features of popular Unix variants (including BSD and System V) when those do not conflict with the standards. Different compatibility modes (selectable when you compile an application) allow the peaceful coexistence of compatibility support for different varieties of Unix.

其他说法

libc 实际上是一个泛指。凡是符合实现了C标准规定的内容,都是一种 libc 。

glibc是GNU组织对libc的一种实现。它是unix/linux的根基之一。

微软也有自己的libc实现,叫msvcrt 。

嵌入式行业里还常用uClibc ,是一个迷你版的libc 。


分享到:


相關文章: