Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么要使用预编译头(C/C+;+;)?_C++_Precompiled Headers - Fatal编程技术网

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

C++ 为什么要使用预编译头(C/C+;+;)?,c++,precompiled-headers,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 eAsciiMs

为什么要使用预编译头


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

#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"
#pragma一次
//定义用于生产版本的
#ifndef生产
#定义eMsg(x)(x)//显示错误消息
#定义eAsciiMsg(x)(x)
#否则
#定义eMsg(x)(L“”)//不显示错误消息
#定义eAsciiMsg(x)(“”)
#endif//生产
#包括“targetver.h”
#包括“version.h”
//启用“不安全”,但要快得多的字符串函数
#定义\u CRT\u安全\u无\u警告
#定义SCL安全无警告
//标准包括
#包括
#包括
#包括
#包括
#包括
#ifdef_调试
#包括
#恩迪夫
//标准模板库
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//增强库
#包括
#包括
#包括
//窗口包括
#定义WIN32_精益_和_平均值
#包括
#包括“FILETIME\u comparations.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“wintrust.lib”)
#pragma注释(lib,“kernel32.lib”)
#pragma注释(lib,“Psapi.lib”)
#pragma注释(lib,“shlwapi.lib”)
#pragma注释(lib,“imagehlp.lib”)
#pragma注释(lib,“Advapi32.lib”)
#pragma注释(lib,“Shell32.lib”)
#pragma注释(lib,“Sfc.lib”)
#pragma注释(lib,“Version.lib”)
//加密++库
#ifdef_调试
#pragma注释(lib,“cryptlibd.lib”)
#否则
#pragma注释(lib,“cryptlib.lib”)
#恩迪夫
#定义CRYPTOPP\u启用\u命名空间\u弱1
#包括
#包括
//字符串库
#包括“StringVersions.h”
#包括“expandEnvStrings.h”
#包括“randomString.h”
#包括“getShortPathName.h”
//正则表达式库
#包括“fpattern.h”
//文件结果记录
#包括“unixTimeToFileTime.h”
#包括“fileData.h”
//作家
#包括“writeFileData.h”
//标准结构体系
#包括“优先事项”
#包括“标准H”
#包括“OPSTRUCT.H”
#包括“regss.H”
#包括“FILTER.h”
//子程序根类
#包括“subgramclass.h”
//全球数据
#包括“globalOptions.h”
//记录器
#包括“logger.h”
//控制台解析器
#包括“consoleParser.h”
//超时处理程序
#包括“timeoutThread.h”
//压缩库
#包括“zip.h”
#包括“unzip.h”
#包括“zipIt.h”
//扫描器
#包括“mainScanner.h”
#包括“filescanner.h”
//子程序
#包括“volumeEnumerate.h”
#包括“clsidCompressor.h”
#包括“times.h”
#包括“exec.h”
#包括“uZip.h”
//64位支持
#包括“禁用64.h”

这样您就不必在每次构建项目时都编译它们。它们用于不会更改的系统标题。

它编译速度更快。没有它们,C++编译需要几年时间。尝试在大型项目中比较一些时间

它加快了编译速度

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

在C/C++中,#include机制是指定到当前文件中的文件的文本副本。标头包括其他头(包括其他头),因此当你做一个包含的,它可以在每个CPP文件中添加成千上万的C++行(或者CXX,C,不管什么),所有这些都需要每次编译。这可能是大型项目的严重瓶颈


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

Re:您当前的使用情况是,如果您有一个包含大量文件的目标,那么以这种方式使用PCH可能会更快—请尝试关闭它们以找到答案。这要视情况而定:如果您有很多自己的头文件,并且您很少更改它们,并且您有大量的源文件,而且更改频率更高,那么您的PCH使用将减少重建时间


但通常的建议是只把永远不会改变的东西放在PCH中,因为生成PCH本身有一定的开销。如果每次重建时(通过不断调整其中一个标题)都会触发此错误,则使用PCH可能会降低重建速度。

Visual Studio上的一个重要注意事项:确保每个标题中的第一个“include”为“stdafx.h”,否则您的文件将不会被解析!愚蠢,我知道。但这是一个很容易犯的错误。更具体地说,上面的任何内容#include“stdafx.h”都不会被解析。看到这些文件几乎每次构建都会发生变化:PI深入了解了预编译头文件的实际原因,因为我们知道头文件本身并不是翻译单元。很好的解释,谢谢。很好的简短解释。这应该被标记为答案。“编译每个标题一次”是什么意思?标题是否已编译?只有声明。。。否?@Gupta好吧,声明(如
intfoo(inta,intb)
)也需要“编译”——即由编译器处理。除此之外,与C中的头相反,C++中的报头也包括实际代码(这是可以说不是最好的主意)。特别是在考虑模板时,通常来说,模板必须位于头文件中。如果我的项目只有一个包含预编译头文件的
.cpp
文件。在这种情况下使用预编译头会加快编译速度吗?如果一个项目很小-少于10个文件,是否仍应使用预编译头?文件有多大?