Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 276
Ben Avatar asked Mar 18 '23 08:03

Ben


1 Answers

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

Edit1Edit2

  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.


like image 126
galdin Avatar answered Apr 25 '23 07:04

galdin