Thread: Visual Studio - "stdafx.h" Questions

  1. #1
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47

    Visual Studio - "stdafx.h" Questions

    I finally got around to installing/starting to use Visual Studio Professional 2013, and I noticed that when I create a new console project I get an:
    Code:
    #include "stdafx.h"
    Some quick googling gave me some information on precompiled headers, but I wanted to check here to see what exactly I should and shouldn't put in this file, and to make sure I understand it completely.

    So from my understanding, "stdafx.h" is a file VS creates and uses as a precompiled header file. The first time you compile your project it will compile the contents of stdafx.h. Every time after the first time you compile, stdafx.h will automatically be included as long as it hadn't changed. So your compile time should get quicker since you won't be compiling all of your normal #include type stuff each time. (Please correct me if I'm wrong)

    Should I put all of the #includes that my program will be using into stdafx.h? Like <iostream>, <string>...etc? What about headers that I create myself that won't be changing much, should they be put in there as well?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There is nothing wrong with putting the STL and boost etc., in your precompiled headers. Templates will be instantiated from what's in the precompiled header. The only real drawback is if a part of the precompiled header changes, then you have a situation where the entire thing is recompiled, which can take a very long time.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You've got it nailed down. The only drawback is as whiteflags mentions, that if something in the PCH changes, you need to recompile the entire file and since it usually contains a lot of heavy headers, it can take a long time. So the general rule of thumb is (for small projects): put all your system headers in there and not your own.

    Remember also that they only help parsing headers. They don't help with instantiating templates. So even if you put your boost (for example) includes in there, if you go crazy on using the library, compile time will still substantially increase because the compiler must instantiate all the templates in the header files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    Thanks for the information! Seems like a pretty cool feature.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with #include "stdafx.h"
    By shiroaisu in forum C++ Programming
    Replies: 12
    Last Post: 07-15-2011, 01:18 AM
  2. Visual Studio - "Single-EXE" output
    By Petike in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2009, 05:54 AM
  3. Visual Studio C++ 2005 first task "Create a window"
    By csonx_p in forum Windows Programming
    Replies: 3
    Last Post: 04-15-2008, 05:49 AM
  4. compiler is not able to locate "stdafx.h"
    By prejudged_fire in forum Windows Programming
    Replies: 4
    Last Post: 01-13-2007, 10:46 AM
  5. Replies: 14
    Last Post: 12-26-2004, 11:18 AM