Ben
Ben

Reputation: 1349

How do I include a header file located in a specific folder? (C++)

I want to include a specific header file (MyHeader.h) in a C++ project. The solution for my project is located in the folder:

C:\\Projects\\MyProgram

The header file is located in the folder:

C:\\Projects\\MyProgram\\Files

I tried the following line of code, but it doesn't work.

#include <Files\MyHeader.h>

Is there an easy way to include the header file without adding the full path to "Include directories" in the configuration properties?

Thanks in advance for any help. :)

Upvotes: 5

Views: 46991

Answers (3)

Erwin R Panganiban
Erwin R Panganiban

Reputation: 27

You just need to replace your brackets <> with double quotes "" like this:

#include "Files\MyHeader.h"

Brackets is used when you want Visual Studio to find the path from your project settings and double quotes when you want to access the header from a specific path or relative to your project.

Upvotes: 0

galdin
galdin

Reputation: 14074

Try this

#include "files/myheader.h"

It will work if the header is in a files folder in the same directory as the current source.


If you're trying to include a 3rd party library and not your own header, I'd suggest you to save the library headers in a particular path (say C:\Library\headers). (If there are static libraries put them in some other path like C:\Library\lib).

  1. In your Visual Studio C++ Project, go to View > Other Windows > Property Manager.

Property Manager Window

  1. Double Click on the Project Name. You will see a dialog box like this:

Property Page Editor

Make sure All Configurations is chosen in the dropdown, if you want the change to be applied to both the Debug and the Release Configurations. Else just choose the Configuration you want the properties to be applied to.

  1. Go to VC++ Directories on the left and choose Include Directories in the right, and enter the path(s) in the textbox separated by a ;.

You can also use the drop down and use the Dialog box to add the paths if you'd prefer to browse to each path separately

Edit1 Edit2

  1. Add the library path the same way to Library Directories
  2. Save the changes using the Save button on the Property Manager Pane's toolbox.

You will then be able to access the header file contained in the directory you added by something like:

#include <myheader.h>

This approach will help, because it won't matter where the headers saved. The header path is not hard-coded.


Upvotes: 5

CashCow
CashCow

Reputation: 31445

The current directory of the source file is always searched, although if you use angled brackets it is searched after your include path, whilst if you use quotes it will be the first directory searched.

The directory of your solution or makefile/project file is irrelevant, the local path is relative to the compilation unit, i.e. the cpp file.

If that cpp file includes a header, that headers own includes are relative to itself, not the cpp file that included it. (It would be hell to manage if it were not).

Ideally you should use forward slashes in paths too.

Your actual correct setup here is to include the solution directory in your search path. If it is Visual Studio you can use a macro for this, $(SolutionDir) I think.

That means that if anyone else is going to build your solution, they can put it in a directory they choose and as long as the structure underneath is the same, it will still work.

To use a relative path in your cpp file without any include directory settings, you might need something like:

#include "../Files/MyHeader.h"

Upvotes: 5

Related Questions