“C or gcc” is like “Chicken or the egg” ? :( [duplicate]

落花浮王杯 提交于 2019-12-30 11:12:12

问题


Possible Duplicate:
How are gcc/g++ bootstrapped?

I would like to know how gcc is compiled as we all know it is written in C.

Did they used some other compiler to come up with gcc?

If so, can I use the same compile to compile my C program?


回答1:


There is no chicken and egg here. glibc is compiled with the compiler you are using.

That compiler was first compiled with a previous version of the same compiler. Then it can compile itself as well.

The real chicken-and-egg problem was solved in the 1950's when someone had to write the world's first compiler. After that, you can use one compiler to compile the next one.




回答2:


There are two basic ways to build a new compiler:

  1. If you're writing a new compiler for an established language like C, use an existing compiler from a different vendor to build your new compiler. For example, you could use the C compiler shipped with HP-UX to build gcc.

  2. If you're writing a compiler for a new language, start by implementing a very simple compiler in a different language (the first C compiler was written in PDP-11 assembler). This initial compiler will only recognize a small subset of the target language; basically enough to do some file I/O and some simple statements. Write a new compiler in the target language subset and build it with your first compiler. Now write a slightly more capable compiler that can recognize a larger subset of the target language, and build it with the second compiler. Repeat the process until you have a compiler capable of recognizing the full target language.




回答3:


They did not use some other compiler. You can write a C program that doesn't use glibc by simply telling the compiler not to use it. So something like this:

gcc main.c -nostdlib



回答4:


This is an interesting question. I think you are wondering in what language is written a compiler of a new language, aren't you? Well, if we had only Assembly language (for instance,x86), the only way to write a C compiler would be in Assembly language. Later, we could write a better, yet more powerful compiler written in C by using our assembly-written compiler, and so on... An the question arises: how did the early programmers write the first assembly compiler? My father told me: by manually entering the 1's and 0's! :-)



来源:https://stackoverflow.com/questions/11798506/c-or-gcc-is-like-chicken-or-the-egg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!