Linux中的libc和glibc

来源:互联网 发布:淘宝网怎么换货 编辑:程序博客网 时间:2024/06/04 21:35

一、libc库

Linux平台提供的C标准库包括:

  • 一组头文件,定义了很多类型和宏,声明了很多库函数。这些头文件放在哪些目录下取决于不同的编译器,stdarg.h和stddef.h位于/usr/lib/gcc/i486-linux-gnu/4.3.2/include目录下,stdio.h、stdlib.h、time.h、math.h、assert.h位于/usr/include目录下。C99标准定义的头文件有24个,本书只介绍其中最基本、最常用的几个。

  • 一组库文件,提供了库函数的实现。大多数库函数在libc共享库中,有些库函数在另外的共享库中,例如数学函数在libm中。在第 4 节 “共享库”讲过,通常libc共享库是/lib/libc.so.6。

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

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

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

    glibc下载地址:http://mirror.hust.edu.cn/gnu/

    glibc是linux下面c标准库的实现,即GNU C Library。glibc本身是GNU旗下的C标准库,后来逐渐成为了Linux的标准c库,而Linux下原来的标准c库Linux libc逐渐不再被维护。Linux下面的标准c库不仅有这一个,如uclibc(https://www.uclibc.org/)、klibc,以及上面被提到的Linux libc,但是glibc无疑是用得最多的。glibc在/lib目录下的.so文件为libc.so.6。


    查看当前系统的 glibc 版本的两种方法: 
    1.
    01[root@Betty ~]# /lib/libc.so.6
    02GNU C Library stable release version 2.5, by Roland McGrath et al.
    03Copyright (C) 2006 Free Software Foundation, Inc.
    04This is free software; see the source for copying conditions.
    05There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
    06PARTICULAR PURPOSE.
    07Compiled by GNU CC version 4.1.2 20080704 (Red Hat 4.1.2-52).
    08Compiled on a Linux 2.6.9 system on 2013-01-08.
    09Available extensions:
    10        The C stubs add-on version 2.1.2.
    11        crypt add-on version 2.1 by Michael Glad and others
    12        GNU Libidn by Simon Josefsson
    13        GNU libio by Per Bothner
    14        NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
    15        Native POSIX Threads Library by Ulrich Drepper et al
    16        BIND-8.2.3-T5B
    17        RT using linux kernel aio
    18Thread-local storage support included.
    19For bug reporting instructions, please see:
    20<http://www.gnu.org/software/libc/bugs.html>.
    21[root@Betty ~]#
    2. 
    1[root@Betty ~]# ldd  --version
    2ldd (GNU libc) 2.5
    3Copyright (C) 2006 Free Software Foundation, Inc.
    4This is free software; see the source for copying conditions.  There is NO
    5warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    6Written by Roland McGrath and Ulrich Drepper.
    7[root@Betty ~]#


    【官方说明】

    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. 

  • 具体使用了什么库,要看你调用的函数了~可能不会仅仅只包含一个库~Linux下,库的路径一般是:/lib,/usr/lib,/usr/local/lib等,这些路径一般会在/etc/ld.so.conf 中标记出来,如果需要添加特殊位置的库,可以把库的路径添加到/etc/ld.so.conf中去,并且执行ldconfig来使得新路径立即生效~ 

0 0
原创粉丝点击