Linux内核:使用什么样的C Linux内核?

时间:2022-12-17 03:09:15

I am confused here. They say linux kernel is developed using C. But to my knowledge, C library is built on top of Linux kernel, so at kernel land, there should be no C just yet. And yet again, the kernel code I saw from GitHub were all written in C, all with those weird includes! It's just like that classic chicken vs egg puzzle to me. Which one exists first?

我在这里很困惑。他们说linux内核是用C开发的。但据我所知,C库是建立在Linux内核之上的,所以在内核领域,应该还没有C。而且,我从GitHub看到的内核代码都是用C语言编写的,所有这些都包含了奇怪的内容!就像那个经典的鸡蛋和鸡蛋拼图一样。首先存在哪一个?

Thanks in advance for your patience with my stupid question(s).

提前感谢您对我的愚蠢问题的耐心等待。

3 个解决方案

#1


3  

C isn't built ontop of linux. C in itself is a compiled programming language, that a compiler translates into machine code. Based on your OS, the compiler may do it differently (for some C code).

C不是构建在linux上的。 C本身是一种编译的编程语言,编译器转换为机器代码。根据您的操作系统,编译器可能会采用不同的方式(对于某些C代码)。

But the language C itself really is just a very long list of things functions should do and how things should behave, and compilers just obey these rules. Thats what is called the C "standard". There is a comittee that sets it, and there are multiple versions.

但是语言C本身实际上只是功能应该做的事情和事情应该如何表现的很长的列表,编译器只是遵守这些规则。这就是所谓的C“标准”。有一个设置它的委员会,有多个版本。

Linux Kernel was indeed written in C. So someone wrote it and then compiled it using a standard-compliant C compiler.

Linux Kernel确实用C编写。所以有人编写它然后使用符合标准的C编译器编译它。

As for libraries, they're optional. The Linux kernel is developed without dependencies, that means it implements everything it needs itself, in plain C. These includes you see are just files from the kernel itself, defining its functions, types etc.

至于图书馆,它们是可选的。 Linux内核是在没有依赖关系的情况下开发的,这意味着它实现了它本身所需的一切,在简单的C中。这些包括你看到的只是内核本身的文件,定义它的功能,类型等。

#2


4  

The linux kernel (and other kernels) is developed freestanding, this means it doesn't use any external libraries. Every function it needs is implemented inside the kernel. What you call "weird includes" are includes declaring its own internal functions and types.

linux内核(和其他内核)是独立开发的,这意味着它不使用任何外部库。它需要的每个功能都在内核中实现。你所谓的“怪异包含”包括声明自己的内部功能和类型。

#3


4  

The C specification makes a distinction between hosted and freestanding implementations. For some details, see Is there a meaningful distinction between freestanding and hosted implementations? and https://*.com/questions/35164489/what-is-the-reason-for-creating-freestanding-vs-hosted-implementation.

C规范区分了托管和独立实现。有关详细信息,请参阅独立实现和托管实现之间是否存在有意义的区别?和https://*.com/questions/35164489/what-is-the-reason-for-creating-freestanding-vs-hosted-implementation。

One of the differences is that freestanding implementations are not required to provide all the standard library functions. When compiling a Unix kernel, we use the compiler in a freestanding mode, because the many of the standard libraries depend on having a kernel beneath them. In particular, the standard I/O library requires an operating system with files, but the kernel is where that all gets implemented, so it can't be used from the kernel.

其中一个不同之处在于,不需要独立实现来提供所有标准库函数。在编译Unix内核时,我们以独立模式使用编译器,因为许多标准库依赖于它们下面的内核。特别是,标准I / O库需要一个带有文件的操作系统,但内核是所有实现的地方,因此无法从内核中使用它。

While there are some library functions, like the ones in <string.h>, that could be the same in the kernel, to keep things simple it doesn't link with any of the standard libraries. There are functions like strcpy() in the kernel, but they're copies of the standard library code, not linked with the same libraries (on many systems, the standard C library is dynamically linked, but this isn't feasible in the kernel).

虽然有一些库函数,比如 中的函数,但在内核中可能是相同的,为了简单起见,它不与任何标准库链接。内核中有strcpy()等函数,但它们是标准库代码的副本,不与相同的库链接(在许多系统上,标准C库是动态链接的,但这在内核中是不可行的)。

So the kernel makes use of the C language, but none of the C libraries.

所以内核使用C语言,但没有使用C库。

#1


3  

C isn't built ontop of linux. C in itself is a compiled programming language, that a compiler translates into machine code. Based on your OS, the compiler may do it differently (for some C code).

C不是构建在linux上的。 C本身是一种编译的编程语言,编译器转换为机器代码。根据您的操作系统,编译器可能会采用不同的方式(对于某些C代码)。

But the language C itself really is just a very long list of things functions should do and how things should behave, and compilers just obey these rules. Thats what is called the C "standard". There is a comittee that sets it, and there are multiple versions.

但是语言C本身实际上只是功能应该做的事情和事情应该如何表现的很长的列表,编译器只是遵守这些规则。这就是所谓的C“标准”。有一个设置它的委员会,有多个版本。

Linux Kernel was indeed written in C. So someone wrote it and then compiled it using a standard-compliant C compiler.

Linux Kernel确实用C编写。所以有人编写它然后使用符合标准的C编译器编译它。

As for libraries, they're optional. The Linux kernel is developed without dependencies, that means it implements everything it needs itself, in plain C. These includes you see are just files from the kernel itself, defining its functions, types etc.

至于图书馆,它们是可选的。 Linux内核是在没有依赖关系的情况下开发的,这意味着它实现了它本身所需的一切,在简单的C中。这些包括你看到的只是内核本身的文件,定义它的功能,类型等。

#2


4  

The linux kernel (and other kernels) is developed freestanding, this means it doesn't use any external libraries. Every function it needs is implemented inside the kernel. What you call "weird includes" are includes declaring its own internal functions and types.

linux内核(和其他内核)是独立开发的,这意味着它不使用任何外部库。它需要的每个功能都在内核中实现。你所谓的“怪异包含”包括声明自己的内部功能和类型。

#3


4  

The C specification makes a distinction between hosted and freestanding implementations. For some details, see Is there a meaningful distinction between freestanding and hosted implementations? and https://*.com/questions/35164489/what-is-the-reason-for-creating-freestanding-vs-hosted-implementation.

C规范区分了托管和独立实现。有关详细信息,请参阅独立实现和托管实现之间是否存在有意义的区别?和https://*.com/questions/35164489/what-is-the-reason-for-creating-freestanding-vs-hosted-implementation。

One of the differences is that freestanding implementations are not required to provide all the standard library functions. When compiling a Unix kernel, we use the compiler in a freestanding mode, because the many of the standard libraries depend on having a kernel beneath them. In particular, the standard I/O library requires an operating system with files, but the kernel is where that all gets implemented, so it can't be used from the kernel.

其中一个不同之处在于,不需要独立实现来提供所有标准库函数。在编译Unix内核时,我们以独立模式使用编译器,因为许多标准库依赖于它们下面的内核。特别是,标准I / O库需要一个带有文件的操作系统,但内核是所有实现的地方,因此无法从内核中使用它。

While there are some library functions, like the ones in <string.h>, that could be the same in the kernel, to keep things simple it doesn't link with any of the standard libraries. There are functions like strcpy() in the kernel, but they're copies of the standard library code, not linked with the same libraries (on many systems, the standard C library is dynamically linked, but this isn't feasible in the kernel).

虽然有一些库函数,比如 中的函数,但在内核中可能是相同的,为了简单起见,它不与任何标准库链接。内核中有strcpy()等函数,但它们是标准库代码的副本,不与相同的库链接(在许多系统上,标准C库是动态链接的,但这在内核中是不可行的)。

So the kernel makes use of the C language, but none of the C libraries.

所以内核使用C语言,但没有使用C库。