Should precompiled header files also be included in source header files?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Should precompiled header files also be included in source header files?

  1. #1
    Join Date
    May 2006
    Location
    England
    Posts
    69

    Should precompiled header files also be included in source header files?

    When including a header file in stdafx.h, should that file still be included in the source file where it is actually used?

    I've done lots of searching but I can't convince myself either way...

    If it is included in both places, is the one in the source file ignored?

    Cheers,
    AnotherMuggle.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Should precompiled header files also be included in source header files?

    As long as you're including stdafx.h, no.

  3. #3
    Join Date
    May 2006
    Location
    England
    Posts
    69

    Re: Should precompiled header files also be included in source header files?

    Quote Originally Posted by GCDEF View Post
    As long as you're including stdafx.h, no.
    Is that, no you shouldn't, or no you don't have to?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Should precompiled header files also be included in source header files?

    Quote Originally Posted by AnotherMuggle View Post
    Is that, no you shouldn't, or no you don't have to?
    Both. #include essentially just inserts the included file into the code where the #include statement is placed. When you #include stdafx.h, you include it, and any files that it #includes, so #including a file in stdafx.h and in your code is redundant. Depending on your use of include guards, the the compiler will use the file the first time it sees it, and either ignore it after that or generate errors.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: Should precompiled header files also be included in source header files?

    When including a header file in stdafx.h, should that file still be included in the source file where it is actually used?
    If a header file is included in stdafx.h and stdafx,h is included in the program, then you don't need to also include the header file in the source file as includes can be nested (ie an include file can have an include file which can have an include file...).

    If it is included in both places, is the one in the source file ignored?
    This depends upon how the include file is coded. If it starts with #pragma once or uses include guards then yes, the one in the source file is ignored. If the include file doesn't take into account being included multiple times then no, its not ignored. The compiler doesn't automatically ignore multi includes.

    Well written header files usually start with

    Code:
    #pragma once
    if the complier supports this

    or an include guard
    Code:
    #ifndef headerxyz
    #define headerxyz
    //include code
    #endif
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    May 2006
    Location
    England
    Posts
    69

    Re: Should precompiled header files also be included in source header files?

    Thanks both for the helpful answers.

    Are static libraries an exception in any way? This issue relates closely to my other thread here: http://forums.codeguru.com/showthrea...static-library but I felt the question was different enough to start a new thread.

    Basically if I include this in stdafx.h:
    Code:
    #ifdef MFC_BUILD
    #include <afx.h>
    #else
    #include <atlstr.h>
    #endif
    but nowhere else in the library then MFC builds compile and link fine. However, non-MFC builds do not compile and the errors all revolve around CString being an undeclared identifier. If I duplicate the above include in each of the headers where CString is used, then the errors go away, the build completes and everything seems fine.

    I'm stumped
    Last edited by AnotherMuggle; September 16th, 2013 at 10:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center