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

发布于 2024-07-20 10:27:08 字数 2840 浏览 8 评论 0原文

为什么要使用预编译头?


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

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

Why use precompiled headers?


Reading the responses, I suspect what I've been doing with them is kind of stupid:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕梦 2024-07-27 10:27:08

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

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

In C/C++, the #include mechanism is a textual copy of the file specified into the current file. Headers include other headers (which include yet other headers), so when you do a #include, it could be adding tens of thousands of lines of C++ into each cpp file (or cxx, c, whatever), all of which need to be compiled each time. This can be a severe bottleneck for large projects.

Precompiled headers speed this up by compiling each header once, then including that compiled state into the cpp they are included in.

五里雾 2024-07-27 10:27:08

它的编译速度要快得多。 如果没有它们,C++ 编译需要花费数年时间。 尝试比较大型项目中的一些时间!

It compiles a lot quicker. C++ compilation takes years without them. Try comparing some time in a large project!

情话墙 2024-07-27 10:27:08

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

但通常的建议是只将永不改变的内容放入 PCH 中,因为生成 PCH 本身会产生一定的开销。 如果您在每次重建时触发该功能(通过不断调整标头之一),则使用 PCH 可能会使重建速度变慢。

Re: your current usage, if you have a target with a very large number of files, it may still be faster to use PCH in that way - try switching them off to find out. It depends: if you have a lot of headers of your own, and you change them only infrequently, and you have a very large number of source files that you change much more frequently, then your PCH usage will cut rebuild times.

But normal advice is to only put things in PCH that never change, because there is a certain overhead to producing the PCH itself. If you trigger that off with every rebuild (by constantly tweaking one of your headers), using PCH may make the rebuild slower.

一场信仰旅途 2024-07-27 10:27:08

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

So you don't have to compile them every time you build your project. They're used for system headers that aren't going to change.

丢了幸福的猪 2024-07-27 10:27:08

它加快了编译速度。

当您包含其他项目的标头时,您不希望更改它们。 如果将它们放入预编译头中,那么当您更改源代码时,无需重新编译该代码。 这减少了未更改代码的重复编译,从而加快了编译时间。

It speeds up compilation.

When you're including headers from other projects, you don't expect to change them. If you put these into a precompiled header, then that code will not have to be recompiled when you make changes to your source code. This reduces repetitive compilation of unchanged code, speeding up compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文