I am working on a game using Visual C++. I have some components in separate projects, and have set the project dependencies. How do I #include a header file from a different project? I have no idea how to use classes from one project in another.
6 Answers
Settings for compiler
In the project where you want to #include the header file from another project, you will need to add the path of the header file into the Additional Include Directories section in the project configuration.
To access the project configuration:
- Right-click on the project, and select Properties.
- Select Configuration Properties->C/C++->General.
- Set the path under Additional Include Directories.
How to include
To include the header file, simply write the following in your code:
#include "filename.h"
Note that you don't need to specify the path here, because you include the directory in the Additional Include Directories already, so Visual Studio will know where to look for it.
If you don't want to add every header file location in the project settings, you could just include a directory up to a point, and then #include relative to that point:
// In project settings
Additional Include Directories ..\..\libroot
// In code
#include "lib1/lib1.h" // path is relative to libroot
#include "lib2/lib2.h" // path is relative to libroot
Setting for linker
If using static libraries (i.e. .lib file), you will also need to add the library to the linker input, so that at linkage time the symbols can be linked against (otherwise you'll get an unresolved symbol):
- Right-click on the project, and select Properties.
- Select Configuration Properties->Linker->Input
- Enter the library under Additional Dependencies.
-
9May I just say that after a morning of reading answers on this subject on SO, yours in the most clear and comprehensive I've come across. Well done and thanks! Apr 13, 2011 at 11:50
-
11There was a suggestion from anonymous user as, "When you include the Path for the library, make sure you enter them in quotes if the path has spaces". Adding it as comment, if it helps anyone.– iDevNov 14, 2012 at 20:23
-
2One additional way to include a static library is, within the solution's "project dependencies", to configure the project to be a dependency of the static library to be linked to.It took me ages to figure out why one of my projects was linking correctly and the other was not - this was why. Nov 28, 2013 at 15:24
-
4I'd like to point out that using "Additional Include Directories" with the other project's source file directory can be a terrible idea. The other project may have files with the same names (very likely if you're using pre-compiled headers for each one). Personally, I prefer to add the parent folder of the projects source files, so you can at least specify yourself, e.g.
#include "proj2\include.h"
. Having multiple projects per solution seems very directed towards the NET languages, as they are used very differently. Yet to find a great way to overcome this for C++ projects.– DejiFeb 6, 2014 at 12:40 -
21This is a bit shoddy. VS can do so much automatically. Hard to believe there isn't a better solution compared to hard coding the path - project dependency setting or the like might be nice.– CookieFeb 12, 2015 at 10:14
Since both projects are under the same solution, there's a simpler way for the include files and linker as described in https://docs.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019 :
- The include can be written in a relative path (E.g.
#include "../libProject/libHeader.h"
). - For the linker, right click on "References", Click on Add Reference, and choose the other project.
-
Nice and simple, but it's too bad the relative path is needed everywhere you include a header.– yoyoApr 22, 2020 at 17:25
-
IMO this is the best answer. The relative path can be simpler if you add "../libProject" to include paths: (VS2015) properties / "C/C++" / additional include paths Sep 14, 2020 at 10:10
-
Expanding on @Benav's answer, my preferred approach is to:
- Add the solution directory to your include paths:
- right click on your project in the Solution Explorer
- select Properties
- select All Configurations and All Platforms from the drop-downs
- select C/C++ > General
- add
$(SolutionDir)
to the Additional Include Directories
- Add references to each project you want to use:
- right click on your project's References in the Solution Explorer
- select Add Reference...
- select the project(s) you want to refer to
Now you can include headers from your referenced projects like so:
#include "OtherProject/Header.h"
Notes:
- This assumes that your solution file is stored one folder up from each of your projects, which is the default organization when creating projects with Visual Studio.
- You could now include any file from a path relative to the solution folder, which may not be desirable but for the simplicity of the approach I'm ok with this.
- Step 2 isn't necessary for
#include
s, but it sets the correct build dependencies, which you probably want.
-
1This is also the best answer for a medium to large codebase, because eventually you end up with header names colliding, or headers with no obvious source:
#include "Retry.h"
This is also how Google does it. it took me a while to come around to this way of thinking, but now that I've used it for a couple years, it's hard to go back. For an example, look at the Google C++ Style Guide under Names and Order of Includes. Nov 16, 2021 at 20:45
#include
has nothing to do with projects - it just tells the preprocessor "put the contents of the header file here". If you give it a path that points to the correct location (can be a relative path, like ../your_file.h) it will be included correctly.
You will, however, have to learn about libraries (static/dynamic libraries) in order to make such projects link properly - but that's another question.
You need to set the path to the headers in the project properties so the compiler looks there when trying to find the header file(s). I can't remember the exact location, but look though the Project properties and you should see it.
-
Location can be under Properties>C/C++>General>Additional Include Directories. Mar 19, 2015 at 13:52
Try to avoid complete path references in the #include directive, whether they are absolute or relative. Instead, add the location of the other project's include folder in your project settings. Use only subfolders in path references when necessary. That way, it is easier to move things around without having to update your code.