How do some of these headers and includes work? - Simple, basic question. - SOLVED

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How do some of these headers and includes work? - Simple, basic question. - SOLVED

      Hey all,

      I picked up GCC4 recently and I've really been enjoying it. Thanks to Mike and Rez for the resouce.

      I've also been following along with the code and as I'm trying to recreate some of the things that are in it, I've run into a small header include problem that has also been a problem in previos projects.

      In BaseGameLogic.h you access things that have been declared in the precompiled header and it's own includes, like StrongActorPtr or even just std::vector. However, you don't include it or anything that holds those definitions in the file anywhere, and yet there are no compile or IntelliSense errors. When I try to do the same, I get errors "'StrongActorPtr' is undefined". Similarly, in your interfaces.h you use AppMsg which has been delcared in the precompiled header, with no errors, even though you do not include it into the interfaces.h. When I do the same, I get errors "error C2061: syntax error : identifier 'AppMsg'". Oddly, I don't have any IntelliSense errors and the word AppMsg is blue as if it is recognized.

      Can someone explain what exactly is going on in the computer when it comes to these header issues? I know that using an include is like copy/pasting all the code from that file to where the include statement is, but I feel like I'm missing some other key characteristics of includes.

      Thanks.
    • Although I do not use PCH's in my own code we use them at work, from what I gather the behaviour of these is to pre-compile anything you put into the header (usually StdAfx.h) and in turn, force every file to include the PCH. Because the PCH has this file included (and is the first header included) this means that any headers that you have are actually included after the PCH file as well, meaning that even if they do not include anything, they will still have access to the PCH definitions that are included before this. I personally do not like relying on order of header inclusion to ensure that the header has access to what it needs, in my opinion if a header is using an object type, it should either forward declare this or explicitly state its dependencies within itself.
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • No, sorry let me rephrase that.

      You are right, every source file is required to compile with the precompiled header, and is required at the top of the file, it effects other header files because it is included before them.
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • Take precompiled headers out of the equation since this is not a property of PCH's. You can do this with any header file.

      Header files are never compiled by themselves. They are only compiled when a CPP file includes them. You can prove that right now by going to any header file in the project and hitting Ctrl+F7 to try to compile it. You can have a header in your project with a syntax error and it'll never be found unless somewhere it's included by a CPP file (or by a header file which in turn is included in a CPP file).

      Now, #include literally replaces that line with the contents of the file. That means that the header order can be important. Let's say I have the following files:

      Source Code

      1. // Vector3.h
      2. class Vector3
      3. {
      4. // .....
      5. };


      Source Code

      1. // Test.h
      2. class Test
      3. {
      4. Vector3 m_position;
      5. };


      C Source Code

      1. // Test.cpp
      2. #include "Test.h"
      3. #include "Vector3.h"
      4. Test g_test;


      This will break. You'll get a compiler error in Test.h saying that it doesn't know what Vector3 is. The reason is because the compiler is actually building it like this:

      Source Code

      1. // Test.h
      2. class Test
      3. {
      4. Vector3 m_position;
      5. };
      6. // Vector3.h
      7. class Vector3
      8. {
      9. // .....
      10. };
      11. // Test.cpp
      12. Test g_test;
      Display All


      See the problem? Vector3 is defined after it's used. If you switch the include order, it will work.

      Now, back to GCC. That header is explicitly included at the top of every source file. Thus, the definitions in that file are valid everywhere in the code base. You can guarantee that no matter what's being compiled, this file is always at the top. You can do this with anything header, including your non-precompiled header.

      Speaking of precompiled headers, they're really just an optimization. There's no magic involved here, all they do is compile the code into an intermediate format similar to the obj's that CPP files are compiled into. That way, the compiler doesn't have to reparse and translate the text a bunch of times.

      Here's the wiki for precompiled headers, which does a pretty good job at explaining them:
      en.wikipedia.org/wiki/Precompiled_header

      -Rez