Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 262
Sergio Avatar asked Jan 15 '11 21:01

Sergio


People also ask

How do I add a header file in Visual Studio?

Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option. The Fix all occurrences dialog will open where you can preview the changes.

How do I include a header file?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.

How do I open a header file in Visual Studio?

Right-click anywhere in your file and select Toggle Header/Code File. Or, you can select Ctrl+K, Ctrl+O.

How do you insert a file in Visual Studio?

You can add existing files to your project by right-clicking on the Project node and selecting Add > Add Files.... Alternatively, to add an entire folder, select Add > Add Existing Folder.... The file browser is shown. It lets you search your system for the required item to add.


1 Answers

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

like image 90
Bojan Komazec Avatar answered Sep 17 '22 15:09

Bojan Komazec