CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2006
    Location
    England
    Posts
    72

    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
    Location
    Florida
    Posts
    12,635

    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
    72

    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
    Location
    Florida
    Posts
    12,635

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    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
  •  





Click Here to Expand Forum to Full Width

Featured