为什么使用预编译头(C / C ++)?

时间:2009-05-24 06:35:24

标签: c++ precompiled-headers

快速提问 - 为什么要使用预编译标题?

编辑: 阅读回复,我怀疑我一直在做的事情有点愚蠢:

 #pragma once

//Defines used for production versions

#ifndef PRODUCTION
#define eMsg(x) (x) //Show error messages
#define eAsciiMsg(x) (x)
#else
#define eMsg(x) (L"") //Don't show error messages
#define eAsciiMsg(x) ("")
#endif //PRODUCTION

#include "targetver.h"
#include "version.h"

//Enable "unsafe" but much faster string functions
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS

//Standard Includes
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <direct.h>
#include <cstring>
#ifdef _DEBUG
#include <cstdlib>
#endif

//Standard Template Library
#include <bitset>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
#include <string>
#include <numeric>

//BOOST libraries
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/scoped_array.hpp>

//Windows Includes
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "FILETIME_Comparisons.h"
#include <shlwapi.h>
#include <Shellapi.h>
#include <psapi.h>
#include <imagehlp.h>
#include <mscat.h>
#include <Softpub.h>
#include <sfc.h>
#pragma comment(lib, "wintrust.lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"Psapi.lib")
#pragma comment(lib,"shlwapi.lib")
#pragma comment(lib,"imagehlp.lib")
#pragma comment(lib,"Advapi32.lib")
#pragma comment(lib,"Shell32.lib")
#pragma comment(lib,"Sfc.lib")
#pragma comment(lib,"Version.lib")

//Crypto ++ Libraries
#ifdef _DEBUG
#pragma comment(lib,"cryptlibd.lib")
#else
#pragma comment(lib,"cryptlib.lib")
#endif
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <md5.h>
#include <sha.h>

//String libraries
#include "stringUnicodeConversions.h"
#include "expandEnvStrings.h"
#include "randomString.h"
#include "getShortPathName.h"

//Regular Expression Libraries
#include "fpattern.h"

//File Result Record
#include "unixTimeToFileTime.h"
#include "fileData.h"

//Writer
#include "writeFileData.h"

//Criteria Structure System
#include "priorities.h"
#include "criterion.H"
#include "OPSTRUCT.H"
#include "regexClass.H"
#include "FILTER.h"

//Sub Programs Root Class
#include "subProgramClass.h"

//Global data
#include "globalOptions.h"

//Logger
#include "logger.h"

//Console parser
#include "consoleParser.h"

//Timeout handler
#include "timeoutThread.h"

//Zip library
#include "zip.h"
#include "unzip.h"
#include "zipIt.h"

//Scanner
#include "mainScanner.h"
#include "filesScanner.h"

//Sub Programs
#include "volumeEnumerate.h"
#include "clsidCompressor.h"
#include "times.h"
#include "exec.h"
#include "uZip.h"

//64 Bit support
#include "disable64.h"

5 个答案:

答案 0 :(得分:47)

在C / C ++中,#include机制是指定到当前文件中的文件的文本副本。标题包括其他标题(包括其他标题),因此当你执行#include时,可能会在每个cpp文件(或cxx,c,无论如何)中添加数万行C ++,所有这些都需要每次编译。这可能是大型项目的严重瓶颈。

预编译头文件通过编译每个头文件一次,然后将编译状态包含在它们包含在的cpp中来加快速度。

答案 1 :(得分:38)

它可以更快地编译。没有它们,C ++编译需要数年时间。尝试在大型项目中进行比较!

答案 2 :(得分:9)

Re:您当前的使用情况,如果您的目标文件数量非常大,那么以这种方式使用PCH可能会更快 - 尝试将其关闭以查找结果。这取决于:如果你有很多自己的标题,并且你很少更改它们,并且你有更多的源文件,你更频繁地更改,那么你的PCH使用将减少重建时间。

但正常的建议是只在PCH中放置永不改变的东西,因为生产PCH本身会有一定的开销。如果你在每次重建时触发它(通过不断调整你的一个标题),使用PCH可能会使重建速度变慢。

答案 3 :(得分:6)

因此,每次构建项目时都不必编译它们。它们用于不会改变的系统头文件。

答案 4 :(得分:6)

加快编译速度。

当您包含其他项目的标题时,您不希望更改它们。如果将这些放入预编译的头文件中,那么在对源代码进行更改时,不必重新编译该代码。这减少了重复编译未更改的代码,加快了编译时间。