Cannot open include file with Visual Studio

2019-02-11 18:41发布

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"

5条回答
成全新的幸福
2楼-- · 2019-02-11 19:01

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.

查看更多
Emotional °昔
3楼-- · 2019-02-11 19:07

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.

查看更多
Fickle 薄情
4楼-- · 2019-02-11 19:07

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.

查看更多
Fickle 薄情
5楼-- · 2019-02-11 19:11

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.

查看更多
forever°为你锁心
6楼-- · 2019-02-11 19:22

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).

查看更多
登录 后发表回答