c++windowsvisual-c++precompiled-headersstdafx.h

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


While my static library is working fine, I noticed the default settings came with the following files:

I don't expect this library to be huge. I wish to minimalize as much as possible. Is stdafx.h/.cpp necessary?

How they appear in my project:

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>

Solution

  • Compiling even a small project may take a lot of time without precompiled headers because any project indirectly includes many SDK and standard library headers. So yes, you need those files and no, you won't get anything useful by removing them. Your project will compile very slowly without them, that's the only result you will get.

    In case you don't know this already - all includes for standard header files like 'windows.h' or 'stdlib.h' must be in 'stdafx.h'. This way the standard headers will not be recompiled on every project build (and for every project file).