繁体   English   中英

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

[英]Why use precompiled headers (C/C++)?

为什么要使用预编译头?


阅读这些回复,我怀疑我对他们所做的事情有点愚蠢:

#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"

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

预编译头通过将每个头编译一次,然后将该编译状态包含到包含它们的 cpp 中来加快此过程。

它编译快了很多 没有它们,C++ 编译需要数年时间。 尝试在大型项目中比较一些时间!

回复:您当前的使用情况,如果您有一个包含大量文件的目标,以这种方式使用 PCH 可能仍然更快 - 尝试将它们关闭以找出答案。 这取决于:如果您有很多自己的标头,并且很少更改它们,并且您有大量的源文件并且更改的频率更高,那么您的 PCH 使用将减少重建时间。

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

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

它加快了编译速度。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM