stdafx.h/.cpp 应该从 C++ 的静态库项目中删除吗?

Should stdafx.h/.cpp be removed from a static library project in C++?

虽然我的静态库工作正常,但我注意到默认设置包含以下文件:

我不希望这个图书馆很大。我希望尽可能地减少。 stdafx.h/.cpp 是必需的吗?

它们如何出现在我的项目中:

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers



// TODO: reference additional headers your program requires here
#include "mathlib.h"   // My main library

stdafx.cpp

// stdafx.cpp : source file that includes just the standard includes
// MathLib.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

targetver.h

#pragma once

// Including SDKDDKVer.h defines the highest available Windows platform.

// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.

#include <SDKDDKVer.h>

如果你不使用预编译的headers,你可以只删除文件,从代码中删除相关的包含。预编译的 header 文件仅在实际使用该功能时才需要。

也在项目属性下->C/C++->预编译headers,select不使用预编译Headers 在预编译 Header 字段中。

如果没有预编译的头文件,即使是一个小项目也可能会花费很多时间,因为任何项目都间接包含许多 SDK 和标准库头文件。所以是的,您需要这些文件,不,删除它们不会得到任何有用的东西。如果没有它们,您的项目将编译得非常慢,这是您将获得的唯一结果。

如果您还不知道这一点 - 所有标准头文件的包含,如 'windows.h' 或 'stdlib.h' 必须在 'stdafx.h' 中。这样,标准头文件就不会在每个项目构建(以及每个项目文件)上重新编译。