31

I have recently gone from Code::Blocks to Visual Studio, and in Code::Blocks one could just add a class and then include it straight away. However, whenever I do the same in Visual Studio with the following statement:

#include "includedFile.h"

or

#include "include/includedFile.h"

It doesn't work and instead I get the error:

cannot open include file: 'includedFile.h'; no such file or directory.

Is there some box or setting that I have to tick? Or do I have to add each header as a dependency manually?

Here is the code for the class in question:

Public.h:

    #pragma once
    class Public
    {
        public:
            static const int SCREEN_WIDTH=1000;
            static const int SCREEN_HEIGHT=1250;
            Public(void);
            ~Public(void);
    };

Public.cpp:

    #include "Public.h"


    Public::Public(void)
    {
    }


    Public::~Public(void)
    {
    }

How it is being included:

    #include "Public.h"
Community
  • 1
  • 1
user2853108
  • 1,291
  • 3
  • 12
  • 15
  • see this comment http://stackoverflow.com/a/31730081/185022 it should be marked as correct solution – AZ_ Oct 10 '16 at 13:00
  • 1
    Check that the configuration and platform (Debug/Release, Win32/x64) matches the configuration and platform of any change you make to solution properties (such as "Additional Include Directories"). – Jiminion Jan 09 '18 at 15:06

9 Answers9

45

I had this same issue going from e.g gcc to visual studio for C programming. Make sure your include file is actually in the directory -- not just shown in the VS project tree. For me in other languages copying into a folder in the project tree would indeed move the file in. With Visual Studio 2010, pasting into "Header Files" was NOT putting the .h file there.

Please check your actual directory for the presence of the include file. Putting it into the "header files" folder in project/solution explorer was not enough.

Yablargo
  • 3,520
  • 7
  • 37
  • 58
43

Go to your Project properties (Project -> Properties -> Configuration Properties -> C/C++ -> General) and in the field Additional Include Directories add the path to your .h file.

And be sure that your Configuration and Platform are the active ones. Example: Configuration: Active(Debug) Platform: Active(Win32).

Radu Ruse
  • 431
  • 4
  • 2
  • 1
    Although this is not the most probable cause of the error in question, it was mine. – Tyson Hilmer Apr 24 '18 at 09:11
  • In case you do not find Project -> Properties -> Configuration Properties -> C/C++ -> General, make sure you have a cpp file in project and do a build (which will fail, but will generate the properties). – Tudor Jan 12 '20 at 12:40
  • 2
    The active configuration was what got me! I was changing the settings for Release and compiling for Debug (VS defaulted to Release even though I had Debug active). It's likely that you want to select "All Configurations" for your include directories. – OLP Feb 07 '20 at 18:07
  • 1
    That second sentence was key! Your a life saver... What a stupid mistake to make :-P Thanks!!! – Christian May 05 '21 at 15:07
  • i can't seem to find where this is :( do you have any hints or links that i can follow through with – minh anh b Aug 15 '22 at 20:05
  • The amount of man hours I've wasted changing settings in Release configuration when I was running in Debug mode (and vice versa). If VS gave a small warning, we'd all be in flying cars by now. – Steve Smith Feb 14 '23 at 13:43
17

You need to set the path for the preprocessor to search for these include files, if they are not in the project folder.

You can set the path in VC++ Directories, or in Additional Include Directories. Both are found in project settings.

JBentley
  • 6,099
  • 5
  • 37
  • 72
Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
  • 13
    They are in the project folder and I have added the path to the project in both directories, alas it doesn't work – user2853108 Oct 17 '13 at 23:31
16

By default, Visual Studio searches for headers in the folder where your project is ($ProjectDir) and in the default standard libraries directories. If you need to include something that is not placed in your project directory, you need to add the path to the folder to include:

Go to your Project properties (Project -> Properties -> Configuration Properties -> C/C++ -> General) and in the field Additional Include Directories add the path to your .h file.

You can, also, as suggested by Chris Olen, add the path to VC++ Directories field.

tomi.lee.jones
  • 1,563
  • 1
  • 15
  • 22
  • 1
    They are in the project folder and I have added the path to the project in both directories, alas it doesn't work – user2853108 Oct 17 '13 at 23:33
  • 3
    Sorry, but it is not possible. It is a very straightforward thing to do, and if you entered correct paths, it should open. If they are in the project folder, don't add the path to the project - you just `#include "file.h"`. Last thing you can do, is to Right click project -> Add -> New item -> Header file and then include it, the file will be created automatically in your project dir. If it doesn't work, something is very wrong with your Visual Studio. – tomi.lee.jones Oct 18 '13 at 06:21
  • 1
    It is, see my edit. Additionally I don't seem to be having any problem with the inclusion in the class belonging to the header, just elsewhere – user2853108 Oct 18 '13 at 23:48
  • @user2853108 What do you mean "problem with the inclusion in the class belonging to the header"? Please clarify. – JBentley Oct 18 '13 at 23:52
  • By that I mean that visual studio was not giving me any error on when I #include in Public, just on the #include in main – user2853108 Oct 19 '13 at 00:07
  • You need to `#include "Public.h"` both in public.cpp and main.cpp. Can I ask You to start the new project (File->New->Project->Visual C++ ->Win32->Win32 Console Application). Be sure to mark "Empty Project". After started , right click on "Source" in the Solution explorer and then Add->New->.cpp file , and make it your main? So we can be sure the problem is not with the basics of using Visual Studio. – tomi.lee.jones Oct 19 '13 at 18:31
  • 1
    Make sure its actually in your include direcotry not just in the project tree. Pasting into the project tree wasn't actually putting a file into the project dir for me! – Yablargo Dec 03 '13 at 02:57
  • The question quotes an error message from the compiler--not the IDE. It's the compiler that cannot find the header. (The IDE does have a way to search for header files from the code editor, but those rules are different.) The compiler doesn't care where the project file is. As far as the compiler is concerned, there may not be a project file (or project directory). For a quoted file name, the compiler should be looking first in the same directory as the file that's trying to include the header, which probably explains why user2853108 says it works from Public.cpp but not main. – Adrian McCarthy Feb 15 '17 at 23:04
  • The compiler cares where the project file is because the IDE tells it to care, via arguments. Visual Studio has a variable, `$(ProjectDir)`, which is the directory where the project file is. `$(ProjectDir)` is included in `Additional Include Directories` (inherited from project defaults). `Additional Include Directories` are passed to the compiler. – cowlinator Feb 20 '19 at 21:33
8

I found this post because I was having the same error in Microsoft Visual C++. (Though it seems it's cause was a little different, than the above posted question.)

I had placed the file, I was trying to include, in the same directory, but it still could not be found.

My include looked like this: #include <ftdi.h>

But When I changed it to this: #include "ftdi.h" then it found it.

Joe
  • 8,251
  • 3
  • 18
  • 23
  • This was the solution for me. Thank you for this! Now I'd like to know what the "<>" are supposed to be for... – Jimmy Oct 05 '15 at 14:40
  • 3
    It seems to me, the `<>` are used for system files and then the quote marks are used for included files in your project. – Joe Oct 06 '15 at 15:19
  • 1
    This is the exact definition of how Visual Studio uses bracket includes (`<>`) versus quote includes (`""`): https://learn.microsoft.com/en-us/cpp/preprocessor/hash-include-directive-c-cpp . Summary: Brackets `<>` will stick strictly to the "include directories" settings. Quotes `""` will also do "include directories", but will first search local project directories. – cowlinator Feb 20 '19 at 21:53
3

For me, it helped to link the projects current directory as such:

In the properties -> C++ -> General window, instead of linking the path to the file in "additional include directories". Put "." and uncheck "inheret from parent or project defaults".

Hope this helps.

mkohler
  • 139
  • 5
3

If your problem is still there it's certainly because you are trying to compile a different version from your current settings.

For example if you set your Additional Include Directories in Debug x64, be sure that you are compiling with the same configuration.

Check this: Build > Configuration Manager... > There is problably something like this in your active solution configuration: Debug x86 (Win32) platform.

Patapoom
  • 794
  • 1
  • 7
  • 16
  • Many thanks. This was my problem. In vc2008 the settings dialog was opened using the current settings. – Valentin H Feb 14 '20 at 13:30
  • i have the same settings as `Debug x64` still the visual studio fails to locate the header files also i have checked whether the location of the desired header file is added in additional include directories – Krishna Oza Aug 23 '23 at 11:58
2

I tried the other answers here as well, but my problem had nothing to do with the include paths or files missing incorrect #includes. I had two configurations, each set to the exact same include directories. One configuration could resolve the includes, the other could not.

After selecting my project and going to Project -> Properties, I selected both configurations through the Configuration dropdown -> Multiple Configurations... option. Comparing the two I found that C/C++ -> Language -> Conformance Mode was different. The "incorrect" configuration had a value of Default for some reason, and switching it to Yes or No allowed the paths to be resolved.

TL;DR: If you have one configuration with the same include directories but the other isn't finding the files, I suggest to try comparing the configurations.

gigabot
  • 66
  • 5
0

If you've tried the other answers and your include file still can't be found, here are some additional debugging steps and sanity-checks:

  • Ensure that you are building to a platform that is supported by your code. (If not, consider removing this platform as a target)
  • Verify that the filename/path is correct. Modify your source code to #include the whole absolute path of the header file instead, and see if the file can be found now. If not, copy-paste the path from your source code into a command line to validate that the file exists at that full path with no typos. Open the header file to ensure you have read access. (Change the source code back when done.)
  • If you've already added the path to Additional Include Directories, try clicking the drop-down combo box for Additional Include Directories, and select <Edit...>. This will show you evaluated values of paths. (If it does not show the correct evaluated values, variables in your path might not be set. Click Macros>> to see variables.) Copy-paste the evaluated path into windows explorer to validate that the path exists.
  • Create a new empty C++ "Windows Console Application" project. Set just the one Include Directory, and #include just the one file in your main.cpp, and see if that builds.
cowlinator
  • 7,195
  • 6
  • 41
  • 61