locked
Add Header folders to build RRS feed

  • Question

  • New to Visual Studios.  Just transferred a a large project to 64bit machine.  Headers and source have their own folders but the source files are only looking for the headers in the source folder.

    includes look like this

    #include stdafx.h

    even though they are not in same folder.  If I change the include to

    #include ..\\stdafx.h  

    it will find it but I would need to do this over 100 times to get them all and then I'm sure I would have to do the rest of the includes.

    Where and how do I make the compiler look in the header folder for the headers?

     

    Thanks

     

    Wednesday, October 19, 2011 4:08 PM

Answers

  • >Where and how do I make the compiler look in the header folder for the headers?

    Have you tried setting the Additional Include Directories option in
    your project's Configuration Properties, C/C++, General page?

    Dave

    Wednesday, October 19, 2011 7:43 PM

All replies

  • >Where and how do I make the compiler look in the header folder for the headers?

    Have you tried setting the Additional Include Directories option in
    your project's Configuration Properties, C/C++, General page?

    Dave

    Wednesday, October 19, 2011 7:43 PM
  • Hi,

    Welcome to the MSDN Forum.

    I agreed with David.
    Click Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories, add the path of the header files here. After doing so, the application will look for the headers from this path.

    I am going to mark David's reply as answer. If you have any questions and doubts, please feel free to let us know. You can even unmark it if it can't solve the problem.

    Have a nice day.

    Best regards,


    Helen Zhao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 26, 2011 8:02 AM
  • First off, precompiled headers (such as stdafx.h) are a special case. In the case of a defined pre-compiled header the MS compiler essentially ignores all the text in the cpp file until it sees a #include directive with the precompiled header name in it. Even then it doesn't try to access the referenced header, it just includes all the declarations and definitions it build when it compiled the prefix header earlier in the build process.

    As a result, and for as long as stdafx.h IS in fact the projects pre-compiled header, the location of stdafx.h does NOT have to be in a standard search path at all.

    In fact, files like this tend to build when precompiled headers are in use:

     

    #include "a real header that wont actually be included.h"
    #include "some other header that doesn't exist.h"
    garbage text that should result in a build error but won't
    #include "bogus path/stdafx.h"
    // The compiler only stars compiling the file from this line.
    

    Second, when the headers are in a different folder to the source files the compiler, one way or another, needs to be told where to look. Either by having explicit relative paths such as in your example.

    I prefer to go to the C/C++ Compiler settings for the project and add paths to the Additional Include folders.

     

        $(SolutionDir)
        $(ProjectDir)
        $(ProjectDir)/include

     

    would ensure that the compiler would automatically look for #included files in the same folder as the .sln file, the same folder as the .vcproj file (the project folder), and the include subfolder of the project folder.

    Assuming that all your project folders are sub folders of the solution folder, the first directive lets you reference headers from other projects in the same solution:

        #include "someOtherProject/someOtherProjectsComponent.h"

    The next two, depending on whether you use folders like "source" and "include" or simply a folder per subcomponent / namespace, let you reference headers in the same project relative to the root of the project, rather than having to work out and specify the relative depth by going ..\..\..

     

        // #include "..\..\..\subComponent\header.h" not needed.
        #include "subComponent/header.h"
    
    
    

     

    Wednesday, October 26, 2011 12:45 PM