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 。


分享到:


相關文章: