9

I am currently trying to compile a simple program that includes two header files. I see them in the Solution Explorer, where I included them through "include existing files". However, when I run my program it get the following error. fatal error C1083: Cannot open include file: 'FileWrite.h': No such file or directory. THe problem is that I see the file included in the Header's folder and in the code I have written:

#include "FileWrite.h"

and then the rest of the program code. Is there something else needed to do so that the compiler can see the header file and link it to the .cpp file I'm trying to compile?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Sergio
  • 91
  • 1
  • 1
  • 2

4 Answers4

16

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

  • FileWrite.h should either be in the same directory as your source code file (.cpp) or
  • Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories) or
  • Path could be set in your VisualStudio - add it to Include Files in Tools->Options->Projects and Solutions->VC++ Directories

Which of these options shell be used depends on whether that header originates from this project (1st option) or some other project (any of other two options).

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • +1 for *Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories)* So awesome! – Sibbs Gambling Oct 07 '13 at 08:57
10

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 would be difficult to figure out dependencies for a project when compiling it on a system different than yours.

DumbCoder
  • 5,696
  • 3
  • 29
  • 40
1

When including files the compiler first looks in the current directory (the directory which contains the source .cpp file) then it looks in the additional include directories. If FileWrite.h isn't in the same directory as your source file check the additional included directories.

In the project's property page look at the additional include directories and see if they include the folder in which FileWrite.h is in.

Motti
  • 110,860
  • 49
  • 189
  • 262
1

You said the file is in the "headers" folder. This could either mean the headers filter or an actual headers directory on the filesystem. When including a file from your own project you need to specify the path from the file you're including into. So, if you had something like so:

src/main.cpp
include/my_object.h

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

That's for directories. The folders you see in your project are called filters and have absolutely no relation to the directory structure of your project unless you force it to. You need to be paying attention to what the structure looks like in windows explorer to ascertain what path to use in an include statement.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125