Cannot open include file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    Cannot open include file?

    I am trying to include a a header in my project but the compiler is complaining that it can't find it when it's in the same directory as Main.cpp.

    I am using Visual C++ 2008 Express Edition, here's the code:
    Code:
    #include "Obj.h"
    and then in the folder is:
    Main.cpp
    Obj.cpp
    Obj.h
    Then all the project files that VC++ makes.

    I'm truly baffled.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Possibly your compiler has a -I (minus.capital-aye) flag that tells it where to look
    for header files; check your manual for details.

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      When you include using quotes, the header file has to be in the present working directory. Where Main.cpp is located is irrelevant.

      For Visual Studio, the present working directory is the folder that contains your project file (.vcproj).

      If your header is not there, you will nee to specify the path to it using your project properties: Properties/C-C++/General/Addtional include directories.

      Comment

      • MrPickle
        New Member
        • Jul 2008
        • 100

        #4
        It is in the same folder as the vcproj

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          How are you creating your project? Please give me the steps.

          I assume you are adding files to a project and not just creating files.

          Comment

          • MrPickle
            New Member
            • Jul 2008
            • 100

            #6
            Originally posted by weaknessforcats
            How are you creating your project? Please give me the steps.

            I assume you are adding files to a project and not just creating files.
            I go file > new > project then do all that stuff

            For Main.cpp I went file > new > file > Visual C++ > C++ file

            then I went project > new class and done all that stuff for Obj.h/.cpp because it's a class.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by weaknessforcats
              When you include using quotes, the header file has to be in the present working directory. Where Main.cpp is located is irrelevant.

              For Visual Studio, the present working directory is the folder that contains your project file (.vcproj).
              This is wrong, where main.cpp is located is relevent, taken from The #include Directive on MSDN

              Originally posted by msdn
              Quoted form
              This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.
              and is the definition I have always been familiar with. The current working directory of the compiler is irrelevant (for a conforming compiler without extensions).

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Banfa
                This is wrong, where main.cpp is located is relevent, taken from The #include Directive on MSDN
                You don't need a main.cpp in a C++ program.
                You don't even need a main() function. That's just the default.

                Therefore, the #include does not operate based on a main of any kind.

                The rule is:

                1) If you use the #include <file>, the file must be located along a predefined path. You predefine paths by adding additional include directories in your project properties.
                2) If you use the #include "file", the file must located in the project folder. That is, the one with the .vcproj project file. Otherwise, the seach will revert to (1) above.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by weaknessforcats
                  You don't need a main.cpp in a C++ program.
                  You don't even need a main() function. That's just the default.

                  Therefore, the #include does not operate based on a main of any kind.
                  You have misunderstood me the only significance of main is that is the name already used in this example. The file is being included from main.cpp.

                  Originally posted by weaknessforcats
                  2) If you use the #include "file", the file must located in the project folder. That is, the one with the .vcproj project file. Otherwise, the seach will revert to (1) above.
                  No this is just not right.

                  If you use #include "file" the headers do not need to be in the project folder but in the same folder that the file you are including from is located in or in the same folder as a file that has previously been included in the current include chain or in a folder on the include path. Exactly as MSDN says here which is the same page as before except for for Visual Studio 2008.

                  Please read it because it confirms what I am saying.

                  The project directory has no special meaning for the purpose of including files.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by Banfa
                    The project directory has no special meaning for the purpose of including files.
                    I read your link and wrote a test program and found that you are correct and that I was not.

                    Thank you for persisting long enough for me to learn the correct process.

                    Be sure to let me know about any other errors you find in my replies.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by weaknessforcats
                      Be sure to let me know about any other errors you find in my replies.
                      :-) I will but to be honest I could grow old before it happens again you almost never make mistakes, this is a very rare occasion.

                      You are a true expert in this field and a real benefit not just to the site as a whole but to me personally. It is only since you joined this site and managed to ram a few truths home to me that I have managed to transition from a C programmer that knows C++ syntax to a programmer that is beginning to understand how to approach programming in C++.

                      I would personally like to thank you for all the hard work you put into this site including the tutorials you have written which I have had occasion to make direct reference to in my day to day work.

                      Comment

                      Working...