-1

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?

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

James Real
  • 87
  • 4
  • *"C library is built on top of Linux kernel"* Are you referring to [**glibc**](https://www.gnu.org/software/libc/)? That is an entirely different story from the programming language – UnholySheep Nov 24 '16 at 08:39
  • I am not sure. Even if it is, what kind of library that offers such weird includes? Are there available for use to mortals like me? – James Real Nov 24 '16 at 08:41
  • [Good read for you](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – LPs Nov 24 '16 at 08:42
  • *"weird includes"* what? I don't know what you are talking about. – UnholySheep Nov 24 '16 at 08:42
  • @Unholy Those includes that I never seen like etc. What kind of library is that? – James Real Nov 24 '16 at 08:47
  • 3
    _C library is built on top of Linux kernel_ did you find out something that refers to Linux into that document? BTW from your question it appears that you should start reading a good c book before to start looking at teh Linux Kernel source code. – LPs Nov 24 '16 at 08:49
  • @LP C book don't mention Linux kernel. Are u sure u understand my question correctly? – James Real Nov 24 '16 at 08:51
  • 3
    _See, none of you are quite sure either. I thought I was the only one._...??????? Yes, sure, after 20 years of firmware and software development I still confused about it.... Study something before typing s*** with your keyboard – LPs Nov 24 '16 at 08:52
  • @LP Do you get my confusion now? Because I am about to ask you about the compiler that is used to compile Linux kernel. Can I use MingW or GCC to do that? – James Real Nov 24 '16 at 09:06
  • 1
    Your confusion stems from the fact that you don't understand the difference between a programming language and libraries (standard or otherwise). What do you think is used to compile the Linux kernel? – UnholySheep Nov 24 '16 at 09:10
  • @UnholySheep Now you are asking me question. Don't u think u should give me answer instead? – James Real Nov 24 '16 at 09:18
  • You are confusing the C language with the C standard library. The C standard library is not necessary to write C code. – Lundin Nov 24 '16 at 11:42

3 Answers3

6

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).

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.

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

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.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • I could live with this answer, because there's a clear separation between the interface language and the library. These two are different things. – James Real Nov 24 '16 at 09:02
  • 2
    @JamesReal there is no definitive c "library". C itself is only a standard for how you should translate C code into machine code. A compiler obeys that. Libraries exist to make lots of work thats already been done available to everyday programmers. The people who wrote Linux didn't have that luxury. In other words, how you know C (with all the standard libraries) is not how they had to use it. – Magisch Nov 24 '16 at 09:05
  • @JamesReal No problem. If your question is now resolved, consider marking one of the two answers to it as accepted to indicate that :) – Magisch Nov 24 '16 at 09:18
  • @JamesReal Thats different from the green checkmark that you can dispense. It indicates to other users that you consider the question "resolved" if you mark one of the answers that way. – Magisch Nov 24 '16 at 09:31
  • 1
    @Magisch, sure there is a C library, and it is defined by the C standard, too. In fact, your answer is only half the storry, Bamar's is much more complete. – Jens Gustedt Nov 24 '16 at 12:49
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.

Art
  • 19,807
  • 1
  • 34
  • 60
  • So what you saying is, Linux is not developed by using C as in C known to day-to-day mortals like you and me. If I understand it correctly, it was developed by using C as an interface language. Am I correct? – James Real Nov 24 '16 at 09:00
  • 3
    @JamesReal C is a programming language. While the standard library is part of the language, there's nothing in the language that forces you to use it. You can use as much or as little of the standard library as you want. The only non-standard things a kernel needs to do is to have a few helper function written in assembler for setting things up and maybe some I/O. The rest is just standard C. – Art Nov 24 '16 at 09:05
  • Very clear. Thanks! So saying that Linux was developed using C (as we know it in user land) is not entirely correct, IMO. If we talk about interface, the kernel could be written in PASCAL or anything and build the C Library on top of that. Is this correct? – James Real Nov 24 '16 at 09:14
  • 2
    @JamesReal No, the Linux kernel was developed using C. The language does not change because of libraries (not) used. But it is possible to write a kernel in other languages (old versions of Windows were partially written in Pascal) – UnholySheep Nov 24 '16 at 09:19
  • @UnholySheep It was developed by using C as the interface language. Not C in it's entirety, because that inclusive terminologies tend to mislead people. But thanks. – James Real Nov 24 '16 at 09:27
  • @JamesReal. Do you know that C libraries, for the most, are also written in C? Do you know that micro firmware is often (almost always now) written in C. It's not true what you say. C in its entirety contains only its instruction set, not the libraries. – Sir Jo Black Nov 24 '16 at 09:44
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://stackoverflow.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.

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).

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

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612