How to include header files in Visual Studio 2008?

I am currently trying to compile a simple program that includes two header files. I see them in Solution Explorer, where I included them through "include existing files". However, when I run my program, it gets the following error. Fatal error C1083: Cannot open file include: 'FileWrite.h': There is no such file or directory. The problem is that I see the file included in the header folder and in the code I wrote:

#include "FileWrite.h" 

and then the rest of the code. Is there anything else to do so that the compiler can see the header file and associate it with the .cpp file that I am trying to compile?

+8
c ++ header-files visual-studio-2008 visual-studio
source share
4 answers

If you write something like #include "FileWrite.h" in your code, you need to make sure that the compiler can find this file. There are three options:

  • The Write.h file must either be in the same directory as the source code file (.cpp), or
  • The path to this header file must be specified in the project properties (in C / C ++ → General → Additional inclusion directories) or
  • The path can be set in your VisualStudio - add it to Include Files in Tools-> Options-> Projects and Solutions-> VC ++ Directories

Which of these option shells should be used depends on whether this title comes from this project (option 1) or any other project (any of the other two parameters).

+15
source share

There are two ways to do this.

1) Only for the current project

 Select your project -> properties -> C/C++ -> General -> Additional Include Directories - Include your header file directory. 

2) For all projects

 Tools -> Options -> VC++ Directories -> Include files - Add the header file directory. 

Refrain from using 2, as it was difficult to determine the dependencies for the project when compiling it on a system other than yours.

+10
source share

When you include files, the compiler first looks in the current directory (the directory that contains the source .cpp file), then it looks in additional include directories. If FileWrite.h is not in the same directory as your source file, check the additional included directories.

On the project properties page, look for additional include directories and see if they include the folder in which FileWrite.h is located.

+1
source share

You said the file is in the headers folder. This may mean a header filter or an actual directory of headers in the file system. When you include a file from your own project, you need to specify the path from the file you are entering. So, if you have something like this:

 src/main.cpp include/my_object.h 

You would use #include "../include/my_object.h" in main.cpp.

This is for directories. The folders that you see in your project are called filters and have absolutely nothing to do with the directory structure of your project if you do not force it. You need to pay attention to what the structure looks like in Windows Explorer to figure out which path to use in the include declaration.

+1
source share

Source: https://habr.com/ru/post/651106/


All Articles