為什麼linux的核心用c不用c++呢?

來源:互聯網
上載者:User
java虛擬機器以及windows都是c++寫的,為什麼linux的核心用c不用c++呢?php解譯器和python解譯器為什麼用c不用c++呢?是不是說明php、python比起java虛擬機器更簡單,linux比起windows更簡單呢?

回複內容:

從技術角度猜測一下:

從對編譯的機器代碼的把握度,c比c++好多了。系統程式員幾乎看著c就沒知道編譯器裡出來的彙編代碼的樣子。而c++編譯器出來的就複雜多了,一個對象this指標,再加上各種虛表,當然還有各種拷貝構造等等。要完全把控確實不容易。(雖然我沒有寫過作業系統核心,但我總覺得作業系統核心就應該把控到彙編,用c只是為了提高開發效率。)

從c++的進階語言特性來說,模板,多態等特性,到底在系統開發中用還是不用呢?這是一個很現實的問題。除非能夠很好的把控這些進階特性,否則輕導致編譯出來的代碼膨脹,重則導致奇怪的問題。而且核心調試和應用程式調試完全不是一回事啊…所以bochs雖然都轉成c++了,但開發說明裡明確要求不允許使用模板。

最後還有一點不能忽略,linux核心是跨平台的,所有的平台都有對應的c語言編譯器,而且應該都最佳化的不錯。而不是所有的平台都有c++編譯器,即使有也不一定最佳化的效率很高。

當然還有其他因素,比如linus的個人好惡…1. Windows核心也是C的。這幾年才開始有一點點C++進去,但基本上只用了C with class。
2. Linus個人非常討厭C++。
3. Linux kernel 1991年開搞的,C++到1998才定型。
4. 語言和寫的東西是否簡單無關。作業系統核心是少數幾個需要 明確知道電腦到底幹了什麼的情形,用 C 是為數不多的選擇。來自linus的官方回答(雖然原問題是問為什麼git是用c而不是c++寫的,但根據其中列的原因,也適用於題主這個問題):
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
首先,我想用兩段"很長的代碼"來做一個開始。
下面這段代碼叫 min.c:
#include /* * min()/max() macros that also do * strict type-checking.. See the * "unnecessary" pointer comparison. */#define min(x,y) ({ \        typeof(x) _x = (x);     \        typeof(y) _y = (y);     \        (void) (&_x == &_y);    \        _x < _y ? _x : _y; })int main(void){        int a = 3, b = 5;        double d1 = 3.14, d2 = 2.718;        printf("min of a & b: %d\n", min(a, b));        printf("min of d1 & d2: %lf\n", min(d1, d2));        return 0;}
這個問題也是當時困擾我的,為什麼Linux kernel沒有引入C++. 各甚用C++重寫,收藏的一個回答。

The reasons specifically for Linux are explained in great detail aspart of the Linux Kernel Mailing List (LKML) Frequently AskedQuestions (FAQ) at:  http://www.tux.org/lkml/Scroll down to section 15 (titled Programming Religion) and read theexplanation. 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 kerneland found it ran slower than when compiled with gcc (many thought itshould have been the same); they are not willing to do this again - Linus makes the final decision and he's decided to stick with CThere is also another good explanation of some of the issues at  http://kerneltrap.org/node/2067which describes one person's attempt to build adapt some C++ code toincorporate into a kernel module.For a historical perspective, Thompson and Ritchie developed Unixusing some of the concepts from Multics and CTSS  http://www.multicians.org/unix.htmlincluding implementation of most operations in a higher order language(Unix - C, Multics - PL/I), common naming of commands, etc. I alsorefer you to  http://cm.bell-labs.com/cm/cs/who/dmr/hist.htmlwhich describes the EARLY development of Unix including the conversionfrom Assembly on the PDP-7 to C on the PDP-11 (near the end of thepaper).Building from that basis, the Unix from AT&T that was widelydistributed and worked on by thousands of people was a C basedoperating system. In the 1970's and 1980's, an alternative to AT&T'sdistribution was developed at UC Berkeley, see  http://en.wikipedia.org/wiki/Berkeley_Software_Distributionfor a relatively short explanation of the development of BSD. The BSDversion eventually did not use ANY of the AT&T code base, but was alsoimplemented in C. At the end of the article is a list of several BSDdescendants including SunOS (now Solaris).That is not to say that some applications are better implemented in anOO 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 procedurallanguage, and at this point operating systems are in that category.If this answer is unclear or somehow incomplete, please make aclarification request.Good luck with your work.  --Maniac
Linus不喜歡C++更底層,更可靠,更穩定。問Linus吧,看看他會不會噴你一臉(▭-▭)✧c++太自由了。寫作業系統核心這東西,你放心嗎?
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.