locked
Major trouble figuring out how to link a header file RRS feed

  • Question

  • I'm using Visual Studio Express 2008. As a starter project to get into writing games, I'm trying to write a 2d shooter. I was hoping to have some real physics interaction when the user of the game shoots things, so I decided to install newton game dynamics. I installed it, and the tutorials that are in the installation directory compile, but when I try to include the headers, I am completely mystified on how to get the compiler to see the path to the header and dll files. My code is simply trying to get the header file and looks like this:

    #include <iostream>

    #include "newton.h"
    #include "stdafx.h"


    using namespace std;

    void main(){

        cout << "Hello World!\n";

    }

    I've gone from trying to link the whole path from c: to trying to copy the files I need into the same folder where the project's root file. Currently, I have stdafx.h in a subfolder in the project's root folder.

    And the compilation error I get is:

    : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory

    I've looked through:
    Tools --> options --> VC++ Directoies

    and I've looked through:
     Properties --> Configuration Properties --> linker --> input

    What I'm looking for is how to tell the compiler to see the files I want it to see. I've been working at it for about 6 hours now and nothing I seem to do works. I've googled and Ive looked through the help documentation and I've tried to follow the instructions, but there seems to be some sort of assumption that if you are looking for instructions on how to link a header file, you already know how to link a header file.

    Please help!
    Monday, September 1, 2008 10:02 PM

Answers

  • if you have precompiled headers (and by default you do) then
    #include "StdAfx.h" should be the first line in your source file.

    You include newton.h BEFORE StdAfx.h, but the compiler will throw away everything before StdAfx.h
    The reason this happens is that StdAfx.h contains headers and other stuff that is common for all source files. This could need a lot of time if the compiler would need to actually perform the including and parsing in every cpp file of your project.

    So it only does that once, when it compiles StdAfx.h.
    The result of that is kept in a precompiled header file (pch).
    And then everywhere the compiler sees an include for StdAfx.h, it throws away everything in teh cpp file up until that point, and includes the pch file instead.

    You could argue that this is silly, but there is a good reason for this.
    Suppose your header files depend on macros (#ifdef ....) then including newton.h before StdAfx.h could alter the macros that are defined, and potentially change the result of including StdAfx.h.
    Since this is not allowed (because it negate the whole point of precompiled headers) the compiler simply removes everything up until that point.

    Granted, it would be useful if the compiler would generate a warning.
    Oh and about <> vs ""
    you use <> for including if the include files are system header files that come with the compiler, and "" for header files that belong to your local project.
    Many compiler allow you to use both, but depending on which you use, the compiler will change the location where it starts to search for your headers.

    • Proposed as answer by Bruno van Dooren Tuesday, September 2, 2008 6:32 AM
    • Marked as answer by Yan-Fei Wei Monday, September 8, 2008 1:51 AM
    Tuesday, September 2, 2008 6:32 AM

All replies

  • The first include file should be "stdafx.h".

    Monday, September 1, 2008 10:05 PM
  • Thanks for the reply. Made the change. Same error. The order of the includes over the main in the tutorials that compile are as follows:

    #include "newton.h"
    #include <stdafx.h>
    #include "tutorial.h"
    #include "HiResTimer.h"
    #include "LevelPrimitive.h"
    #include "RenderPrimitive.h"


    And it compiles fine. I don't see where the difference is under the links sections.

    I've also tried <...> as well as "..."

    Same error.
    • Edited by johhnytrash Monday, September 1, 2008 10:54 PM left out 'that compile'
    Monday, September 1, 2008 10:53 PM
  • Well, do you have an stdafx.h file? Usually when you create the project the wizard creates it for you, but if you didn't use the wizard, you won't have one.

    If you don't have one, simply create a new project, selecting the Win32 Console Application, and make sure the "Empty project" option is unchecked. Then edit the *.cpp file appropriately.
    Monday, September 1, 2008 11:13 PM
  • if you have precompiled headers (and by default you do) then
    #include "StdAfx.h" should be the first line in your source file.

    You include newton.h BEFORE StdAfx.h, but the compiler will throw away everything before StdAfx.h
    The reason this happens is that StdAfx.h contains headers and other stuff that is common for all source files. This could need a lot of time if the compiler would need to actually perform the including and parsing in every cpp file of your project.

    So it only does that once, when it compiles StdAfx.h.
    The result of that is kept in a precompiled header file (pch).
    And then everywhere the compiler sees an include for StdAfx.h, it throws away everything in teh cpp file up until that point, and includes the pch file instead.

    You could argue that this is silly, but there is a good reason for this.
    Suppose your header files depend on macros (#ifdef ....) then including newton.h before StdAfx.h could alter the macros that are defined, and potentially change the result of including StdAfx.h.
    Since this is not allowed (because it negate the whole point of precompiled headers) the compiler simply removes everything up until that point.

    Granted, it would be useful if the compiler would generate a warning.
    Oh and about <> vs ""
    you use <> for including if the include files are system header files that come with the compiler, and "" for header files that belong to your local project.
    Many compiler allow you to use both, but depending on which you use, the compiler will change the location where it starts to search for your headers.

    • Proposed as answer by Bruno van Dooren Tuesday, September 2, 2008 6:32 AM
    • Marked as answer by Yan-Fei Wei Monday, September 8, 2008 1:51 AM
    Tuesday, September 2, 2008 6:32 AM
  • Apart from above suggestion .Are you able to see stdafx.h in your File view .If not click on add to project and Select stdafx.h and after that again compile your project by including stdafx.h on top as mention in above project it will work

    Thanx

    Rupesh Shukla
    Tuesday, September 2, 2008 6:53 AM