close

原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Names_and_Order_of_Includes

C\C++在Compile時,是以每個.cpp file為單位(稱為translation unit),透過預處理指令#include把所有需要的symbol都引進來;預處理指令#include "Foo.h",只是單純的把header file Foo.h中的內容,原封不動地貼過來而以。

每個translation unit會被compile成一個.obj檔(以binary型式存在),再透過linker收集互相參考的binary code(有可能是其他的.obj檔,或是另外的.lib檔),並填上參考的位址,最後將所有的binary code合併為.exe檔。

規定#include header file的順序,可以增加可讀性。假若你有一個foo.cpp,Google C++ Style Guide規定#include的順序為以下幾個段落:
1. foo.h
2. C system files
3. C++ system files
4. Other libraries' .h files
5. Your project's .h files

這樣的好處是,當你的foo.h在編譯階段出現錯誤時,會馬上停止編譯,並顯示Error Message,而不會讓你誤以為錯誤是由其他無辜的header file所產生。

而按照2, 3, 4, 5的順序排列的原因是,將比較容易出現Bug的header file放在後面,以免造成該header file的Error出現在其他無辜的header file裡。

 

在每一個段落之中,建議可以按照字母來排列順序。

arrow
arrow
    文章標籤
    C++
    全站熱搜
    創作者介紹
    創作者 coherence 的頭像
    coherence

    打字猴

    coherence 發表在 痞客邦 留言(0) 人氣()