visual c++: #include files from other projects in the same solution
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.
Solution 1:
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.
Solution 2:
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.
Solution 3:
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.