-2

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

  • stdafx.h
  • stdafx.cpp
  • targetver.h

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>
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Bob
  • 327
  • 3
  • 12
  • 2
    Removing `stdafx.h/.cpp` won't affect the size of your library, or would make it leaner in any other ways. – πάντα ῥεῖ Jul 16 '18 at 16:43
  • If I keep it in, I assume I should be doing these things? a) Whenever I make a new cpp file, the only #include should be stdafx.h? b) In stdafx.h, I include all of my library .h files? – Bob Jul 16 '18 at 16:45
  • Did you fully grasp what enabling precompiled headers exactly does? In the end it will reduce the compile time spent on big projects, that's all of its purpose. – πάντα ῥεῖ Jul 16 '18 at 16:47
  • It is there with the presumption that your library some day *might* be huge. Invariably a decent guess, they never get smaller. Default project templates and build settings encourage good practices that suits everybody, throwing them away requires extra work. – Hans Passant Jul 16 '18 at 16:47
  • I do understand them. I don't understand if I need them, hence my question. – Bob Jul 16 '18 at 16:50
  • I would actually say it is there with the presumption that if someday you needed it for whatever reason then it's a (mild) PITA to add to your sources later ... – davidbak Jul 16 '18 at 16:50
  • I may just keep them then. I still have some questions: a) Whenever I make a new cpp file, the only #include should be stdafx.h? b) In stdafx.h, I include all of my library .h files? – Bob Jul 16 '18 at 16:51
  • @bob - no, you don't put in _all_ of your library .h files - you put in those .h files that _most_ of your library .cpp files #include. That may or may not be any particular .h file of your library, and there may very well be more .h files from _outside_ your library in there - standard library headers, boost headers, OS headers, whatever you're using throughout ... – davidbak Jul 16 '18 at 16:52
  • @davidbak Ok, so the commonly used headers go there. For example if I got this right, if my array class requires a custom linked list, and the linked list is only being used for that purpose, the linked list header would go in the array file. It wouldn't be placed in stdafx.h. – Bob Jul 16 '18 at 16:56
  • @bob - right, there's no advantage - to clarify - if that custom linked list is used in your array.cpp only - and if it is used in your array.h, well, you still don't need it explicitly in the stdafx.h, either the array.h will be in there because it is frequently used or it isn't. – davidbak Jul 16 '18 at 16:58

2 Answers2

2

If you do not use precompiled headers, you can just remove the files, remove the relavant includes from the code. The precompiled header files are only necessary if the functionality is actually used.

Also under project properties -> C/C++ -> Precompiled headers, select Not Using Precompiled Headers in the Precompiled Header field.

1

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).

freim
  • 596
  • 2
  • 10