简体   繁体   English

我对C库感到困惑

[英]I'm confused with C libraries

Ok here's the thing. 好的,这就是事情。

Most people learn about the C standard library simultaneously as they first get in contact with the C language and I wasn't an exception either. 大多数人在第一次接触C语言时同时了解C标准库,我也不例外。 But as I am studying linux now, I tend to get confused with C libraries. 但是当我正在研究linux时,我倾向于与C库混淆。 well first, I know that you get a nice old C standard lib as you install gcc on your linux distro as a static lib. 首先,我知道你在Linux发行版上安装gcc作为一个静态库,你会得到一个很好的旧C标准库。 After that, you get a new stable version of glibc pretty soon as you connect to the internet. 在那之后,当你连接到互联网时,你很快就会得到一个新的稳定版本的glibc。

I started to look into glibc API and here's where I got messed up. 我开始研究glibc API,这里我搞砸了。 glibc seems to support vast amount of lib basically starting from POSIX C Standard lib (which implements the standard C lib(including C99 as I know of)) to it's own extensions based on the POSIX standard C lib. glibc似乎支持大量的lib,基本上从POSIX C Standard lib(它实现标准的C lib(包括我知道的C99))到它自己的基于POSIX标准C lib的扩展。

Does this mean that glibc actually modified or added functions in the POSIX C Standard lib? 这是否意味着glibc实际修改或添加了POSIX C标准库中的函数? or even add whole new header set? 甚至添加全新的标题集? Cause I see some functions that are not in the standard C lib but actually included in the standard C header (such as strnlen() in 因为我看到一些函数不在标准C lib中但实际包含在标准C头中(例如strnlen()in

Also referring to what I mentioned about a 'glibc making whole new header set', is because I'm starting to see some header files that seems pretty unique such as linux/blahblah.h or sys/syscalls.h <= (are these the libs that only glibc support?) 还提到我提到的关于'glibc制作全新标题集'的内容,是因为我开始看到一些看起来非常独特的头文件,例如linux / blahblah.h或sys / syscalls.h <=(这些是只有glibc支持的库?)

Next Ques is that I actually heard linux is built based on C language. 接下来的问题是我实际听说linux是基于C语言构建的。 Does this mean linux compiles itself with it's own gcc compiler??????? 这是否意味着linux使用它自己的gcc编译器编译自己????????

For the first question, glibc follows both standard C and POSIX, from About glibc 对于第一个问题,glibc遵循标准C和POSIX,来自About glibc

The GNU C Library is primarily designed to be a portable and high performance C library. GNU C库主要设计为便携式高性能C库。 It follows all relevant standards including ISO C11 and POSIX.1-2008. 它遵循所有相关标准,包括ISO C11和POSIX.1-2008。 It is also internationalized and has one of the most complete internationalization interfaces known. 它也是国际化的,并且具有已知的最完整的国际化接口之一。

For the second question, yes, you can compile Linux using gcc. 对于第二个问题,是的,您可以使用gcc编译Linux。 Even gcc itself can be compiled using gcc, it's called bootstrapping . 甚至gcc本身也可以用gcc编译,它叫做bootstrapping

Glibc implements the POSIX, ANSI and ISO C standards, and adds its own 'fluff', which it calls "glibc extensions". Glibc实现了POSIX,ANSI和ISO C标准,并添加了自己的'fluff',它称之为“glibc扩展”。 The reason that they are all "mixed together" is because they wrote the library as one package, there is no separate POSIX-only glibc. 它们全部“混合在一起”的原因是因为它们将库编写为一个包,没有单独的POSIX-only glibc。

<linux/blah> is not part of glibc. <linux/blah>不是glibc的一部分。 It is a set headers written specifically for the operating system, by people outside of glibc, to give the programmer access to the Linux kernel API. 它是由glibc以外的人专门为操作系统编写的集合头,使程序员可以访问Linux内核API。 It is "part" of the Linux kernel and is installed with it, and is used for kernel hacking. 它是Linux内核的“组成部分”,随之安装,用于内核黑客攻击。 <sys/blah> is part of glibc, and is specific to Linux. <sys/blah>是glibc的一部分,特定于Linux。 It gives access to a fairly abstracted Linux system API. 它可以访问相当抽象的Linux系统API。

As for your second question, yes. 至于你的第二个问题,是的。 Linux is written in C, as it is (according to Linus) the only programming language for kernel and system programming. Linux是用C语言编写的,因为它是(根据Linus)内核和系统编程的唯一编程语言。 The way this is done is through a technique called bootstrapping, where a small compiler is built (usually manually in ASM) and builds the entire kernel or the entirety of GCC. 这样做的方法是通过一种称为bootstrapping的技术,其中构建一个小编译器(通常在ASM中手动)并构建整个内核或整个GCC。

There is one more thing to be aware of: one of the purposes of the libc is to abstract from the actual system kernel. 还有一件事需要注意:libc的一个目的是从实际的系统内核中抽象出来。 As such, the libc is the one part of your app that is kernel specific. 因此,libc是应用程序中特定于内核的一部分。 If you had a different kernel with different syscalls, you would need to have a specially compiled libc. 如果你有一个具有不同系统调用的不同内核,则需要一个特殊编译的libc。 AFAIK, the libc is therefore usually linked as a shared library. 因此,AFAIK通常将libc链接为共享库。

On linux, we usually have the glibc installed, because linux systems usually are GNU/Linux systems with a GNU toolchain on top of the linux kernel. 在linux上,我们通常安装了glibc,因为linux系统通常是GNU / Linux系统,在Linux内核之上有一个GNU工具链。

And yes, the glibc does expand the standards in certain spots: The asprintf() function for instance originated as a gnu-addition. 是的,glibc确实在某些地方扩展了标准: asprintf()函数例如起源于gnu-addition。 It almost made it into the C11 standard subsequently, but until it becomes part of them, it's use will require a glibc-based system, or statically linking with the glibc. 它随后几乎成为C11标准,但直到它成为它们的一部分,它的使用将需要一个基于glibc的系统,或静态链接glibc。

By default, the glibc headers do not define these gnu additions. 默认情况下,glibc标头不会定义这些gnu添加项。 You can switch them on by defining the preprocessor macro GNU_SOURCE before including the appropriate headers, or by specifying -std=gnu11 to the gcc call. 您可以通过在包含适当的标头之前定义预处理器宏GNU_SOURCE或通过将-std=gnu11指定给gcc调用来打开它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM