127

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.

Robino
  • 4,530
  • 3
  • 37
  • 40
rlbond
  • 65,341
  • 56
  • 178
  • 228

6 Answers6

224

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:

  1. Right-click on the project, and select Properties.
  2. Select Configuration Properties->C/C++->General.
  3. 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):

  1. Right-click on the project, and select Properties.
  2. Select Configuration Properties->Linker->Input
  3. Enter the library under Additional Dependencies.
LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111
  • 12
    May 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! – David Hall Apr 13 '11 at 11:50
  • 12
    There 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. – iDev Nov 14 '12 at 20:23
  • 2
    One 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. – Stuart Wood Nov 28 '13 at 15:24
  • 4
    I'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. – Deji Feb 06 '14 at 12:40
  • 3
    What I cant seem to understand is how do you actually tell the linker where the definitions are (the other project `.cpp`) if it doesn't have a `.lib` nor `.dll` ? Right now i have a linker error where the linker doesnt find the definition because I dont know what to put in the `Additional Libraries` – Steinfeld Jul 01 '14 at 09:29
  • 24
    This 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. – Cookie Feb 12 '15 at 10:14
  • I have the same problem as Steinfeld. How can i tell the linker i want to use the cpp files from a project which is creating an executable? – Sonic Feb 02 '16 at 15:15
  • It's worth noting, since I had this issue, to get the C/C++ tab in the properties page for a library, you must have a .cpp file in your project. My project is purely a template, so I had a .h and a .inl (basically just a header with implementation in it, such as inline and templates). I added an empty .cpp file just to get to that option. – Kit10 Apr 01 '16 at 17:49
  • 1
    A reminder for vs 2015 users. In the Project property pages, one may want to select "All Configurations" and " All Platforms" in order to have this to work. – user2165 Nov 27 '16 at 20:25
  • 2
    @Cookie, Shoddy is an understatement. .NET projects do this in a far more elegant way. CodeWarrior, a now defunct product from 90s, allowed you to configure which subprojects to search and their order in an intuitive graphical interface. The classic Macintosh and its software was way ahead of its time. – ATL_DEV Mar 12 '17 at 19:57
  • 1
    I still have the same problem as @Steinfeld. my other project I want to just be a lib. But i want it to be cross platform. Visual studio is saying entrypoint of the other project must be defined. also my main project says unresolved external symbol for all methods. These answers seem very incomplete! – christopher clark Jun 07 '19 at 13:52
11

Since both projects are under the same solution, there's a simpler way for the include files and linker as described in https://learn.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019 :

  1. The include can be written in a relative path (E.g. #include "../libProject/libHeader.h").
  2. For the linker, right click on "References", Click on Add Reference, and choose the other project.
Benav
  • 468
  • 5
  • 5
10

Expanding on @Benav's answer, my preferred approach is to:

  1. 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
  2. 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 #includes, but it sets the correct build dependencies, which you probably want.
yoyo
  • 8,310
  • 4
  • 56
  • 50
  • 1
    This 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. – Jim Beveridge Nov 16 '21 at 20:45
4

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

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
2

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.

Rosstified
  • 4,047
  • 2
  • 25
  • 33
1

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.

cdonner
  • 37,019
  • 22
  • 105
  • 153