先行編譯標頭檔 StdAfx.h

來源:互聯網
上載者:User

標籤:blog   http   使用   io   檔案   ar   代碼   html   

先行編譯標頭檔:

最常見的使用情境就是 StdAfx.h 檔案,在這個檔案中包含常用的標頭檔,比如windows.h,cstdio,string,別的 .cpp 檔案去包含 StdAfx.h 標頭檔。編譯的時候 StdAfx.h 儘管被多個 .cpp 包含,但只會編譯一次。加快了編譯速度。
StdAfx.cpp 專門用來產生先行編譯檔案,StdAfx.cpp 裡只有一行代碼 #include "StdAfx.h", 編譯 StdAfx.cpp 會產生一個 .pch 檔案。別的 .cpp 檔案編譯的時候就會使用這個 .pch 檔案。
貌似還有進階使用情境,現在還沒有瞭解。

先行編譯頭的使用方法:
1. 建立先行編譯標頭檔 StdAfx.h, 在這個標頭檔中包含希望包含的標頭檔,注意不要包含自己的標頭檔,只包含系統標頭檔,不會發生變動。
2. 建立 StdAfx.cpp ,在這個檔案中包含 StdAfx.h
3. 我們要讓編譯器知道, StdAfx.cpp 檔案是用來建立 .pch 檔案的。在 StdAfx.cpp 檔案上面右鍵 --> 屬性 --> 所有配置 --> C/C++ --> 先行編譯頭 --> 選擇 : 建立先行編譯頭(/Yc)
4. 我們要讓編譯器知道,哪些 .cpp 檔案需要使用 .pch 檔案。在需要使用 .pch 檔案的 .cpp 中添加 #include "StdAfx.h", 然後在 .cpp 檔案上右鍵 --> 屬性 --> 所有配置 --> C/C++ --> 先行編譯頭 --> 選擇 : 使用先行編譯頭(/Yu)
5. 單獨編譯 StdAfx.cpp 就會產生 .pch 檔案了,如果以後不小心刪除了 .pch 檔案,可以單獨編譯一下 StdAfx.cpp 檔案。

注意:
1. 並不是所有的 .cpp 檔案都要包含 StdAfx.h ,是否使用先行編譯頭是可以在配置項中修改的。
2. 如果一個 .cpp 檔案使用了先行編譯標頭檔,那麼要在 .cpp 檔案的最開頭去包含 StdAfx.h 檔案,否則會跳過 #include "StdAfx.h" 之前的 #include 代碼。出現 warning C4627: “#include "lua_notifyicon.h"”: 在尋找先行編譯頭使用時跳過

參考:

http://38288890.blog.163.com/blog/static/19612845320081164495863/

http://www.cnblogs.com/magic-cube/archive/2011/12/06/2278568.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.