選擇 Console Application 和 C 專案,接著打上專案名稱,並按確定。 3.專案檔請存成.dev 檔案。 4.接著會得到一個 main.c 的檔案,這是 Dev-C 幫我們弄好的,但不是我們要的東西,請關閉這個檔案:檔案→關閉檔案,並選擇不儲存檔案。.
給定一個pathname,stat函數返回一個與此命名檔有關的資訊結構,fstat函數獲得已在描述項filedes上打開的檔的有關資訊。 lstat函數類似于stat,但是當命名的檔是一個符號連接時,lstat返回該符號連接的有關資訊,而不是由該符號連接引用的檔的資訊。. 註:標頭檔 中.h 可省略。 範例3 :開啟 readme.txt 檔 (同上題),自檔案開始一行一行讀取, 讀取全部資料, 並顯示出,執行結果同上。 註:使用 file.getline(buffer) 可讀取 一行的檔案資料。. CSV檔案的全名是Comma Separated Values 也就是以逗號分隔的資料欄位的文字檔案 因為Excel可以直接開啟CSV檔案,輸出實驗數據很方便 用C/C也可以很方便的處理(最方便的還是Tab分隔的文字檔). 本篇介紹 C/C 中使用 rand 函數產生亂數的方法,並且提供各種常用的範例程式碼。. 在撰寫 C/C 程式時,如果需要產生一些簡單的亂數,最方便的作法就是使用 rand 這個亂數產生函數,以下介紹這個函數的相關用法與範例。.
-->從 Visual Studio 建立新項目時,將加入名為pch.h的預寫標頭檔。When you create a new project in Visual Studio, a precompiled header file named pch.h is added to the project.(在 Visual Studio 2017 和更早版本中,該檔稱為stdafx.h.)該檔的目的是加快生成過程。(In Visual Studio 2017 and earlier, the file was called stdafx.h.) The purpose of the file is to speed up the build process.任何穩定的標頭檔(例如標準庫標頭(如
<vector>
)都應在此處包含。Any stable header files, for example Standard Library headers such as <vector>
, should be included here.僅當預編譯標頭或其包含的任何檔被修改時,才會編譯它。The precompiled header is compiled only when it, or any files it includes, are modified.如果僅在專案原始程式碼中進行更改,則生成將跳過預編譯標頭的編譯。If you only make changes in your project source code, the build will skip compilation for the precompiled header.預寫標頭的編譯器選項為/Y。The compiler options for precompiled headers are /Y.在項目屬性頁中,選項位於設定屬性> C/C++>预编译标头。In the project property pages, the options are located under Configuration Properties > C/C++ > Precompiled Headers.您可以選擇不使用預編譯的標頭,也可以指定標頭檔名以及輸出檔的名稱和路徑。You can choose to not use precompiled headers, and you can specify the header file name and the name and path of the output file.
自訂預編譯碼Custom precompiled code
對於需要大量時間構建的大型專案,您可能需要考慮創建自訂預編譯檔。For large projects that take significant time to build, you may want to consider creating custom precompiled files.Microsoft C 和 C++ 編譯器提供對任何 C 或 C++ 程式碼進行先行編譯的選項,包括內嵌程式碼。The Microsoft C and C++ compilers provide options for precompiling any C or C++ code, including inline code.您可以使用這項效能功能來編譯穩定的程式碼主體,並將程式碼的編譯狀態儲存在檔案中,然後在後續編譯期間,將先行編譯的程式碼與仍在開發中的程式碼結合。Using this performance feature, you can compile a stable body of code, store the compiled state of the code in a file, and, during subsequent compilations, combine the precompiled code with code that is still under development.由於穩定的程式碼不需要重新編譯,因此每個後續編譯的速度會更快。Each subsequent compilation is faster because the stable code does not need to be recompiled.
先行編譯原始程式碼的時機When to Precompile Source Code
預編譯代碼在開發週期中非常有用,用於縮短編譯時間,尤其是在以下情況:Precompiled code is useful during the development cycle to reduce compilation time, especially if:
- 您始終使用不常更改的大量代碼。You always use a large body of code that changes infrequently.
- 您的程式包含多個模組,所有這些模組都使用一組標準的包含檔和相同的編譯選項。Your program comprises multiple modules, all of which use a standard set of include files and the same compilation options.在這種情況下,所有包含檔都可以預編譯為一個預編譯標頭。In this case, all include files can be precompiled into one precompiled header.
第一個編譯(創建預編譯標頭 (PCH) 檔)比後續編譯長一點。The first compilation — the one that creates the precompiled header (PCH) file — takes a bit longer than subsequent compilations.通過包括預編譯的代碼,後續編譯可以更快地進行。Subsequent compilations can proceed more quickly by including the precompiled code.
您可以預編譯 C 和C++程式。You can precompile both C and C++ programs.在C++程式設計中,通常的做法是將類介面資訊分離到頭檔中。In C++ programming, it is common practice to separate class interface information into header files.這些標頭檔以後可以包含在使用類的程式中。These header files can later be included in programs that use the class.通過預編譯這些標頭,可以減少程式編譯所需的時間。By precompiling these headers, you can reduce the time a program takes to compile.
注意
儘管每個源檔只能使用一個預編譯的標頭 (.pch) 檔,但可以在專案中使用多個 .pch 檔。Although you can use only one precompiled header (.pch) file per source file, you can use multiple .pch files in a project.
先行編譯程式碼的兩個選擇Two Choices for Precompiling Code
您可以預編譯任何 C 或C++代碼;您不限於僅預編譯標頭檔。You can precompile any C or C++ code; you are not limited to precompiling only header files.
預編譯需要規劃,但如果預編譯簡單標頭檔以外的原始程式碼,則預編譯速度會明顯加快。Precompiling requires planning, but it offers significantly faster compilations if you precompile source code other than simple header files.
當您知道原始碼使用常用的標頭檔集,但不按相同的順序包括它們,或者當您希望在預編譯中包括原始碼時,請預編譯代碼。Precompile code when you know that your source files use common sets of header files but don't include them in the same order, or when you want to include source code in your precompilation.
預編譯標頭選項為/Yc(創建預編譯的標頭檔)和/Yu(使用預編譯的標頭檔)。The precompiled-header options are /Yc (Create Precompiled Header File) and /Yu (Use Precompiled Header File).使用 /Yc創建預編譯標頭。Use /Yc to create a precompiled header.當與可選的hdrstop實用素一起使用時 ,/Yc允許您預編譯標頭檔和原始程式碼。When used with the optional hdrstop pragma, /Yc lets you precompile both header files and source code.選擇 /Yu以在現有編譯中使用現有的預編譯標頭。Select /Yu to use an existing precompiled header in the existing compilation.您還可以使用 /Fp與 /Yc和 /Yu選項一起為預編譯標頭提供替代名稱。You can also use /Fp with the /Yc and /Yu options to provide an alternative name for the precompiled header.
/Yu和 /Yc的編譯器選項參考主題討論如何在開發環境中存取此功能。The compiler option reference topics for /Yu and /Yc discuss how to access this functionality in the development environment.
先行編譯標頭的一致性規則Precompiled Header Consistency Rules
由於 PCH 檔包含有關電腦環境的資訊以及有關該程式的記憶體位址資訊,因此應僅在創建該程式的電腦上使用 PCH 檔。Because PCH files contain information about the machine environment as well as memory address information about the program, you should only use a PCH file on the machine where it was created.
針對每個檔案使用的先行編譯標頭的一致性規則Consistency Rules for Per-File Use of Precompiled Headers
/Yu編譯器選項允許您指定要使用的 PCH 檔。The /Yu compiler option lets you specify which PCH file to use.
使用 PCH 檔時,編譯器將假定在創建 PCH 檔時有效的編譯環境(使用一致的編譯器選項、雜注等)有效,除非您另有說明。When you use a PCH file, the compiler assumes the same compilation environment — one that uses consistent compiler options, pragmas, and so on — that was in effect when you created the PCH file, unless you specify otherwise.如果編譯器檢測到不一致,它會發出警告,並儘可能識別不一致。If the compiler detects an inconsistency, it issues a warning and identifies the inconsistency where possible.此類警告不一定表示 PCH 檔有問題;因此,這些警告並不一定表示 PCH 檔存在問題。他們只是警告你潛在的衝突。Such warnings do not necessarily indicate a problem with the PCH file; they simply warn you of possible conflicts.PCH檔的一致性要求如下所述。Consistency requirements for PCH files are described in the following sections.
編譯器選項一致性Compiler Option Consistency
使用 PCH 檔時,以下編譯器選項可能會觸發不一致警告:The following compiler options can trigger an inconsistency warning when using a PCH file:
- 使用預處理器 (/D) 選項創建的巨集在創建 PCH 檔和當前編譯的編譯之間必須相同。Macros created using the Preprocessor (/D) option must be the same between the compilation that created the PCH file and the current compilation.未檢查已定義的常量的狀態,但如果更改這些常量,則可能會出現不可預知的結果。The state of defined constants is not checked, but unpredictable results can occur if these change.
- PCH 檔案不適用於 /E 和 /EP 選項。PCH files do not work with the /E and /EP options.
- 在使用 PCH 檔的後續編譯可以使用這些選項之前,必須使用生成瀏覽資訊 (/FR) 選項或排除局部變數 (/Fr) 選項創建 PCH 檔。PCH files must be created using either the Generate Browse Info (/FR) option or the Exclude Local Variables (/Fr) option before subsequent compilations that use the PCH file can use these options.
C 7.0 相容 (/Z7)C 7.0-Compatible (/Z7)
如果此選項在創建 PCH 檔時有效,則使用 PCH 檔的後續編譯可以使用調試資訊。If this option is in effect when the PCH file is created, subsequent compilations that use the PCH file can use the debugging information.
如果在創建 PCH 檔時 C 7.0 相容 (/Z7) 選項無效,則使用 PCH 檔和 /Z7 的後續編譯將觸發警告。If the C 7.0-Compatible (/Z7) option is not in effect when the PCH file is created, subsequent compilations that use the PCH file and /Z7 trigger a warning.除錯資訊放置在目前的 .obj 檔中,並且在 PCH 檔中定義的本地符號對除錯器不可用。The debugging information is placed in the current .obj file, and local symbols defined in the PCH file are not available to the debugger.
包括路徑一致性Include Path Consistency
PCH 檔不包含有關建立時有效的包含路徑的資訊。A PCH file does not contain information about the include path that was in effect when it was created.使用 PCH 檔時,編譯器始終使用當前編譯中指定的包含路徑。When you use a PCH file, the compiler always uses the include path specified in the current compilation.
來源檔案一致性Source File Consistency
指定「使用預編譯標頭檔 (/Yu)」選項時,編譯器將忽略將預編譯的原始程式碼中顯示的所有預處理器指令(包括雜註)。When you specify the Use Precompiled Header File (/Yu) option, the compiler ignores all preprocessor directives (including pragmas) that appear in the source code that will be precompiled.此類預處理器指令指定的編譯必須與創建預編譯標頭檔 (/Yc) 選項的編譯相同。The compilation specified by such preprocessor directives must be the same as the compilation used for the Create Precompiled Header File (/Yc) option.
實用一致性Pragma Consistency
在創建 PCH 檔期間處理的實用主義通常會影響隨後使用 PCH 檔的檔。Pragmas processed during the creation of a PCH file usually affect the file with which the PCH file is subsequently used.和
comment``message
雜注不會影響編譯的其餘部分。The comment
and message
pragmas do not affect the remainder of the compilation.這些雜注僅影響 PCH 檔中的代碼;它們不會影響隨後使用 PCH 檔案的代碼:These pragmas affect only the code within the PCH file; they do not affect code that subsequently uses the PCH file:
comment | page | subtitle |
linesize | pagesize | title |
message | skip |
這些雜注作為預編譯標頭的一部分保留,並影響使用預編譯標頭的編譯的其餘部分:These pragmas are retained as part of a precompiled header, and affect the remainder of a compilation that uses the precompiled header:
alloc_text | include_alias | pack |
auto_inline | init_seg | pointers_to_members |
check_stack | inline_depth | setlocale |
code_seg | inline_recursion | vtordisp |
data_seg | intrinsic | warning |
function | optimize |
/Yc 和 /Yu 的一致性規則Consistency Rules for /Yc and /Yu
使用使用 /Yc 或 /Yu 創建的預編譯標頭時,編譯器會將當前編譯環境與創建 PCH 檔時存在的編譯環境進行比較。When you use a precompiled header created using /Yc or /Yu, the compiler compares the current compilation environment to the one that existed when you created the PCH file.請確保為當前編譯指定與前一個環境一致的環境(使用一致的編譯器選項、雜注等)。Be sure to specify an environment consistent with the previous one (using consistent compiler options, pragmas, and so on) for the current compilation.如果編譯器檢測到不一致,它會發出警告,並儘可能識別不一致。If the compiler detects an inconsistency, it issues a warning and identifies the inconsistency where possible.此類警告不一定表示 PCH 檔有問題;因此,這些警告並不一定表示 PCH 檔存在問題。他們只是警告你潛在的衝突。Such warnings don't necessarily indicate a problem with the PCH file; they simply warn you of possible conflicts.以下各節解釋預編譯標頭的一致性要求。The following sections explain the consistency requirements for precompiled headers.
編譯器選項一致性Compiler Option Consistency
此表列出了在使用預編譯標頭時可能引發不一致警告的編譯器選項:This table lists compiler options that might trigger an inconsistency warning when using a precompiled header:
選項Option | 名稱Name | 規則Rule |
---|---|---|
/D/D | 定義常量和巨集Define constants and macros | 創建預編譯標頭的編譯和當前編譯之間必須相同。Must be the same between the compilation that created the precompiled header and the current compilation.未檢查已定義的常量的狀態,但如果文件依賴於更改的常量的值,則可能會出現不可預知的結果。The state of defined constants is not checked, but unpredictable results can occur if your files depend on the values of the changed constants. |
/E 或 /EP/E or /EP | 將預處理器輸出複製到標準輸出Copy preprocessor output to standard output | 預編譯標頭不適用於 /E 或 /EP 選項。Precompiled headers do not work with the /E or /EP option. |
/Fr 或 /FR/Fr or /FR | 產生微軟源瀏覽器資訊Generate Microsoft Source Browser information | 對於 /Fr 和 /FR 選項在 /Yu 選項中有效,它們也必須在創建預編譯標頭時有效。For the /Fr and /FR options to be valid with the /Yu option, they must also have been in effect when the precompiled header was created.使用預編譯標頭的後續編譯也會生成源瀏覽器資訊。Subsequent compilations that use the precompiled header also generate Source Browser information.瀏覽器資訊放置在單個 .sbr 檔中,其他檔以與 CodeView 資訊相同的方式引用這些資訊。Browser information is placed in a single .sbr file and is referenced by other files in the same manner as CodeView information.您不能覆蓋源瀏覽器資訊的位置。You cannot override the placement of Source Browser information. |
/GA、/GD、/GE、/Gw 或 /GW/GA, /GD, /GE, /Gw, or /GW | Windows 協定選項Windows protocol options | 創建預編譯標頭的編譯和當前編譯之間必須相同。Must be the same between the compilation that created the precompiled header and the current compilation.如果這些選項不同,則會生成警告消息。If these options differ, a warning message results. |
/ZI/Zi | 產生完整的除錯資訊Generate complete debugging information | 如果此選項在創建預編譯標頭時有效,則使用預編譯的後續編譯可以使用該調試資訊。If this option is in effect when the precompiled header is created, subsequent compilations that use the precompilation can use that debugging information.如果在創建預編譯標頭時 /Zi 無效,則使用預編譯和 /Zi 選項的後續編譯將觸發警告。If /Zi is not in effect when the precompiled header is created, subsequent compilations that use the precompilation and the /Zi option trigger a warning.除錯資訊放置在目前的物件檔中,並且在預編譯標頭中定義的本機號對除錯器不可用。The debugging information is placed in the current object file, and local symbols defined in the precompiled header are not available to the debugger. |
注意
預編譯標頭工具僅用於 C 和 C++源檔。The precompiled header facility is intended for use only in C and C++ source files.
在專案中使用先行編譯的標頭Using Precompiled Headers in a Project
前面的各節概述了預編譯標頭:/Yc 和 /Yu、/Fp 選項和hdrstop雜注。Previous sections present an overview of precompiled headers: /Yc and /Yu, the /Fp option, and the hdrstop pragma.本節介紹在專案中使用手動預編譯標頭選項的方法;它以一個範例 makefile 及其管理的代碼結束。This section describes a method for using the manual precompiled-header options in a project; it ends with an example makefile and the code that it manages.
對於在專案中使用手動預編譯標頭選項的另一種方法,請研究在 Visual Studio 的預設設定期間創建的 MFC_SRC 目錄中的一個 makefile。For another approach to using the manual precompiled-header options in a project, study one of the makefiles located in the MFCSRC directory that is created during the default setup of Visual Studio.這些 makefile 採用與本節中介紹的方法類似的方法,但更多地利用了 Microsoft 程式維護實用程式 (NMAKE) 宏,並更好地控制了生成過程。These makefiles take a similar approach to the one presented in this section but make greater use of Microsoft Program Maintenance Utility (NMAKE) macros, and offer greater control of the build process.
建置程序中的 PCH 檔PCH Files in the Build Process
軟體項目的代碼庫通常包含在多個 C 或 C++源檔、物件檔、庫和標頭檔中。The code base of a software project is usually contained in multiple C or C++ source files, object files, libraries, and header files.通常,makefile 將這些元素組合為可執行檔。Typically, a makefile coordinates the combination of these elements into an executable file.下圖顯示了使用預編譯標頭檔的 makefile 的結構。The following figure shows the structure of a makefile that uses a precompiled header file.此關係圖中的 NMAKE 宏名和檔名與PCH 範例 Makefile和PCH 範例代碼中的範例代碼中的那些名稱一致。The NMAKE macro names and the file names in this diagram are consistent with those in the example code found in Sample Makefile for PCH and Example Code for PCH.
Dev C 標頭檔 Download
該圖使用三個圖表設備來顯示生成過程的流。The figure uses three diagrammatic devices to show the flow of the build process.命名矩形表示每個檔或宏;三個宏表示一個或多個檔。Named rectangles represent each file or macro; the three macros represent one or more files.已對區域表示每個編譯或連結操作。Shaded areas represent each compile or link action.箭號顯示在編譯或連結過程中組合的文件和宏。Arrows show which files and macros are combined during the compilation or linking process.
使用預編譯標頭檔案的 Makefile 的結構Structure of a Makefile That Uses a Precompiled Header File
從關係圖的頂部開始,STABLEHDRS 和 BOUNDRY 都是 NMAKE 宏,其中列出了不需要重新編譯的檔。Beginning at the top of the diagram, both STABLEHDRS and BOUNDRY are NMAKE macros in which you list files not likely to need recompilation.這些檔案由命令字串編譯These files are compiled by the command string
CL /c /W3 /Yc$(BOUNDRY) applib.cpp myapp.cpp
僅當預編譯的標頭檔 (STABLE.pch) 不存在或對兩個巨集中列出的檔進行更改時。only if the precompiled header file (STABLE.pch) does not exist or if you make changes to the files listed in the two macros.在這兩種情況下,預編譯的標頭檔將僅包含來自 STABLEHDRS 宏中列出的檔的代碼。In either case, the precompiled header file will contain code only from the files listed in the STABLEHDRS macro.列出要在 BOUNDRY 宏中預編譯的最後一個檔。List the last file you want precompiled in the BOUNDRY macro.
在這些宏中列出的檔可以是標頭檔或 C 或 C++原始檔。The files you list in these macros can be either header files or C or C++ source files.(單個 PCH 檔不能同時用於 C 和 C++ 模組。請注意,您可以使用hdrstop宏在 BOUNDRY 檔中的某個點停止預編譯。(A single PCH file cannot be used with both C and C++ modules.) Note that you can use the hdrstop macro to stop precompilation at some point within the BOUNDRY file.有關詳細資訊,請參閱hdrstop。See hdrstop for more information.
APPLIB.obj 繼續向下表示最終應用程式中使用的支援代碼。Continuing down the diagram, APPLIB.obj represents the support code used in your final application.它創建自 APPLIB.cpp,UNSTABLEHDRS 宏中列出的檔,並從預編譯標頭中預編譯代碼。It is created from APPLIB.cpp, the files listed in the UNSTABLEHDRS macro, and precompiled code from the precompiled header.
MYAPP.obj 代表您的最終申請。MYAPP.obj represents your final application.它創建於 MYAPP.cpp,即 UNSTABLEHDRS 宏中列出的檔,並從預編譯標頭中預編譯代碼。It is created from MYAPP.cpp, the files listed in the UNSTABLEHDRS macro, and precompiled code from the precompiled header.
最後,可執行檔(MYAPP)。EXE)是通過連結 OBJS 宏(APPLIB.obj 和 MYAPP.obj)中列出的文件創建的。Finally, the executable file (MYAPP.EXE) is created by linking the files listed in the OBJS macro (APPLIB.obj and MYAPP.obj). How reliable is vst crack.
PCH 的 Makefile 範例Sample Makefile for PCH
以下 makefile 使用巨集與 !如果!還!ENDIF 控制流命令結構,以簡化其對項目的適應。The following makefile uses macros and an !IF, !ELSE, !ENDIF flow-of-control command structure to simplify its adaptation to your project.
除了生成過程中 PCH 檔中的「使用預編譯的標頭檔的 Makefile 的結構」中所示的 STABLEHDRS、BOUNDRY 和 UNSTABLEHDRS 宏外,此 makefile 提供了 CLFLAGS 宏和 LINKFLAGS 宏。Aside from the STABLEHDRS, BOUNDRY, and UNSTABLEHDRS macros shown in the figure 'Structure of a Makefile That Uses a Precompiled Header File' in PCH Files in the Build Process, this makefile provides a CLFLAGS macro and a LINKFLAGS macro.您必須使用這些宏來列出編譯器和連結器選項,這些選項適用於生成應用程式的可執行檔的調試版本還是最終版本。You must use these macros to list compiler and linker options that apply whether you build a debug or final version of the application's executable file.還有一個 LIBS 宏,您可以在其中列出專案所需的庫。There is also a LIBS macro where you list the libraries your project requires.
Dev C 標頭檔 Online
makefile 也使用 。如果!還!ENDIF 用於偵測您是否在 NMAKE 命令列上定義 DEBUG 符號:The makefile also uses !IF, !ELSE, !ENDIF to detect whether you define a DEBUG symbol on the NMAKE command line:
此功能使您能夠在開發和程式的最終版本中使用相同的 makefile - 將 DEBUG_0 用於最終版本。This feature makes it possible for you to use the same makefile during development and for the final versions of your program — use DEBUG=0 for the final versions.以下命令列等效:The following command lines are equivalent:
Auto tune up app. 有關製作檔案的詳細資訊,請參閱NMAKE 參考。For more information on makefiles, see NMAKE Reference.另請參考MSVC 編譯器選項與MSVC 連結器選項。Also see MSVC Compiler Options and the MSVC Linker Options.
PCH 範例程式碼Example Code for PCH
以下來源檔用於生成過程中 PCH 檔中描述的 makefile 和PCH 的「範例使檔案」。The following source files are used in the makefile described in PCH Files in the Build Process and Sample Makefile for PCH.請注意,註釋包含重要資訊。Note that the comments contain important information.
另請參閱See also
C/C++ 建置參考C/C++ Building Reference
MSVC 編譯器選項MSVC Compiler Options
MSVC 編譯器選項MSVC Compiler Options