java虚拟机以及windows都是c++写的,为什么linux的内核用c不用c++呢?php解释器和python解释器为什么用c不用c++呢?是不是说明php、python比起java虚拟机更简单,linux比起windows更简单呢?
<code class="language-text">On Wed, 5 Sep 2007, Dmitry Kakurin wrote:
>
> When I first looked at Git source code two things struck me as odd:
> 1. Pure C as opposed to C++. No idea why. Please don't talk about portability,
> it's BS.
*YOU* are full of bullshit.
C++ is a horrible language. It's made more horrible by the fact that a lot
of substandard programmers use it, to the point where it's much much
easier to generate total and utter crap with it. Quite frankly, even if
the choice of C were to do *nothing* but keep the C++ programmers out,
that in itself would be a huge reason to use C.
In other words: the choice of C is the only sane choice. I know Miles
Bader jokingly said "to piss you off", but it's actually true. I've come
to the conclusion that any programmer that would prefer the project to be
in C++ over C is likely a programmer that I really *would* prefer to piss
off, so that he doesn't come and screw up any project I'm involved with.
C++ leads to really really bad design choices. You invariably start using
the "nice" library features of the language like STL and Boost and other
total and utter crap, that may "help" you program, but causes:
- infinite amounts of pain when they don't work (and anybody who tells me
that STL and especially Boost are stable and portable is just so full
of BS that it's not even funny)
- inefficient abstracted programming models where two years down the road
you notice that some abstraction wasn't very efficient, but now all
your code depends on all the nice object models around it, and you
cannot fix it without rewriting your app.
In other words, the only way to do good, efficient, and system-level and
portable C++ ends up to limit yourself to all the things that are
basically available in C. And limiting your project to C means that people
don't screw that up, and also means that you get a lot of programmers that
do actually understand low-level issues and don't screw things up with any
idiotic "object model" crap.
So I'm sorry, but for something like git, where efficiency was a primary
objective, the "advantages" of C++ is just a huge mistake. The fact that
we also piss off people who cannot see that is just a big additional
advantage.
If you want a VCS that is written in C++, go play with Monotone. Really.
They use a "real database". They use "nice object-oriented libraries".
They use "nice C++ abstractions". And quite frankly, as a result of all
these design decisions that sound so appealing to some CS people, the end
result is a horrible and unmaintainable mess.
But I'm sure you'd like it more than git.
Linus
</code>
首先,我想用两段"很长的代码"来做一个开始。<code class="language-c"><span class="cp">#include <stdio.h></stdio.h></span>
<span class="cm">/*</span>
<span class="cm"> * min()/max() macros that also do</span>
<span class="cm"> * strict type-checking.. See the</span>
<span class="cm"> * "unnecessary" pointer comparison.</span>
<span class="cm"> */</span>
<span class="cp">#define min(x,y) ({ \</span>
<span class="cp"> typeof(x) _x = (x); \</span>
<span class="cp"> typeof(y) _y = (y); \</span>
<span class="cp"> (void) (&_x == &_y); \</span>
<span class="cp"> _x
<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="kt">double</span> <span class="n">d1</span> <span class="o">=</span> <span class="mf">3.14</span><span class="p">,</span> <span class="n">d2</span> <span class="o">=</span> <span class="mf">2.718</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"min of a & b: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">min</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">));</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"min of d1 & d2: %lf</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">min</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span> <span class="n">d2</span><span class="p">));</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</span></code>
这个问题也是当时困扰我的,为什么Linux kernel没有引入C++. 各甚用C++重写,收藏的一个回答。<code class="language-text">The reasons specifically for Linux are explained in great detail as
part of the Linux Kernel Mailing List (LKML) Frequently Asked
Questions (FAQ) at:
http://www.tux.org/lkml/
Scroll down to section 15 (titled Programming Religion) and read the
explanation. A summary is:
- Linus started with an 386 computer, Minix, and gcc (no g++ available)
- C is used instead of Assembly for a LOT of reasons (maintenance,
readability, efficiency, ...)
- it is not clear how an object oriented language helps in OS design
- MANY years ago, they tried using g++ to compile the Linux kernel
and found it ran slower than when compiled with gcc (many thought it
should have been the same); they are not willing to do this again
- Linus makes the final decision and he's decided to stick with C
There is also another good explanation of some of the issues at
http://kerneltrap.org/node/2067
which describes one person's attempt to build adapt some C++ code to
incorporate into a kernel module.
For a historical perspective, Thompson and Ritchie developed Unix
using some of the concepts from Multics and CTSS
http://www.multicians.org/unix.html
including implementation of most operations in a higher order language
(Unix - C, Multics - PL/I), common naming of commands, etc. I also
refer you to
http://cm.bell-labs.com/cm/cs/who/dmr/hist.html
which describes the EARLY development of Unix including the conversion
from Assembly on the PDP-7 to C on the PDP-11 (near the end of the
paper).
Building from that basis, the Unix from AT&T that was widely
distributed and worked on by thousands of people was a C based
operating system. In the 1970's and 1980's, an alternative to AT&T's
distribution was developed at UC Berkeley, see
http://en.wikipedia.org/wiki/Berkeley_Software_Distribution
for a relatively short explanation of the development of BSD. The BSD
version eventually did not use ANY of the AT&T code base, but was also
implemented in C. At the end of the article is a list of several BSD
descendants including SunOS (now Solaris).
That is not to say that some applications are better implemented in an
OO language such as C++. There are several Graphical User Interface
(GUI) toolkits such as KDE which are implemented in C++. However,
there are applications that are better suited for a more procedural
language, and at this point operating systems are in that category.
If this answer is unclear or somehow incomplete, please make a
clarification request.
Good luck with your work.
--Maniac
</code>
Linus不喜欢C++
更底层,更可靠,更稳定。
问Linus吧,看看他会不会喷你一脸(▭-▭)✧
c++太自由了。写操作系统内核这东西,你放心么?