對於需要大量時間來建置的大型專案,您可能想要考慮建立自定義先行編譯的檔案。 Microsoft C 和 C++ 編譯器提供對任何 C 或 C++ 程式碼進行先行編譯的選項,包括內嵌程式碼。 使用此效能功能,您可以編譯穩定的程式代碼主體、將程式代碼的編譯狀態儲存在檔案中,並在後續編譯期間,將先行編譯的程式代碼與仍在開發中的程式代碼結合。 因為穩定程序代碼不需要重新編譯,因此每個後續的編譯速度都比較快。
先行編譯原始程式碼的時機
先行編譯的程式代碼在開發周期期間很有用,以減少編譯時間,特別是:
您一律會使用大量不常變更的程式代碼主體。
您的程式包含多個模組,這些模組全都使用一組標準 include 檔案和相同的編譯選項。 在此情況下,所有 Include 檔案都可以先行編譯成一個先行編譯的標頭。 如需處理包含檔案之較新方式的詳細資訊,請參閱 比較標頭單位、模組和先行編譯標頭。
# Makefile : Illustrates the effective use of precompiled
# headers in a project
# Usage: NMAKE option
# option: DEBUG=[0|1]
# (DEBUG not defined is equivalent to DEBUG=0)
#
OBJS = myapp.obj applib.obj
# List all stable header files in the STABLEHDRS macro.
STABLEHDRS = stable.h another.h
# List the final header file to be precompiled here:
BOUNDRY = stable.h
# List header files under development here:
UNSTABLEHDRS = unstable.h
# List all compiler options common to both debug and final
# versions of your code here:
CLFLAGS = /c /W3
# List all linker options common to both debug and final
# versions of your code here:
LINKFLAGS = /nologo
!IF "$(DEBUG)" == "1"
CLFLAGS = /D_DEBUG $(CLFLAGS) /Od /Zi
LINKFLAGS = $(LINKFLAGS) /COD
LIBS = slibce
!ELSE
CLFLAGS = $(CLFLAGS) /Oselg /Gs
LINKFLAGS = $(LINKFLAGS)
LIBS = slibce
!ENDIF
myapp.exe: $(OBJS)
link $(LINKFLAGS) @<<
$(OBJS), myapp, NUL, $(LIBS), NUL;
<<
# Compile myapp
myapp.obj : myapp.cpp $(UNSTABLEHDRS) stable.pch
$(CPP) $(CLFLAGS) /Yu$(BOUNDRY) myapp.cpp
# Compile applib
applib.obj : applib.cpp $(UNSTABLEHDRS) stable.pch
$(CPP) $(CLFLAGS) /Yu$(BOUNDRY) applib.cpp
# Compile headers
stable.pch : $(STABLEHDRS)
$(CPP) $(CLFLAGS) /Yc$(BOUNDRY) applib.cpp myapp.cpp
// ANOTHER.H : Contains the interface to code that is not// likely to change.//#ifndef __ANOTHER_H#define __ANOTHER_H#include<iostream>voidsavemoretime( void );
#endif// __ANOTHER_H
來源檔案 STABLE.H:
C++
// STABLE.H : Contains the interface to code that is not likely// to change. List code that is likely to change// in the makefile's STABLEHDRS macro.//#ifndef __STABLE_H#define __STABLE_H#include<iostream>voidsavetime( void );
#endif// __STABLE_H
來源檔案 UNSTABLE.H:
C++
// UNSTABLE.H : Contains the interface to code that is// likely to change. As the code in a header// file becomes stable, remove the header file// from the makefile's UNSTABLEHDR macro and list// it in the STABLEHDRS macro.//#ifndef __UNSTABLE_H#define __UNSTABLE_H#include<iostream>voidnotstable( void );
#endif// __UNSTABLE_H
來源檔案 APPLIB.CPP:
C++
// APPLIB.CPP : This file contains the code that implements// the interface code declared in the header// files STABLE.H, ANOTHER.H, and UNSTABLE.H.//#include"another.h"#include"stable.h"#include"unstable.h"usingnamespacestd;
// The following code represents code that is deemed stable and// not likely to change. The associated interface code is// precompiled. In this example, the header files STABLE.H and// ANOTHER.H are precompiled.voidsavetime( void ){ cout << "Why recompile stable code?\n"; }
voidsavemoretime( void ){ cout << "Why, indeed?\n\n"; }
// The following code represents code that is still under// development. The associated header file is not precompiled.voidnotstable( void ){ cout << "Unstable code requires"
<< " frequent recompilation.\n";
}
來源檔案 MYAPP.CPP:
C++
// MYAPP.CPP : Sample application// All precompiled code other than the file listed// in the makefile's BOUNDRY macro (stable.h in// this example) must be included before the file// listed in the BOUNDRY macro. Unstable code must// be included after the precompiled code.//#include"another.h"#include"stable.h"#include"unstable.h"intmain( void ){
savetime();
savemoretime();
notstable();
}