visual c++: #include files from other projects in the same solution

143,339

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:

  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.

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 :

  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.

Solution 3

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.

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

Solution 5

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.

Share:
143,339
rlbond
Author by

rlbond

I like C++. I also like Python.

Updated on April 23, 2020

Comments

  • rlbond
    rlbond about 4 years

    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.

  • David Hall
    David Hall about 13 years
    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!
  • iDev
    iDev over 11 years
    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.
  • Stuart Wood
    Stuart Wood over 10 years
    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.
  • Deji
    Deji over 10 years
    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.
  • Steinfeld
    Steinfeld almost 10 years
    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
  • Cookie
    Cookie about 9 years
    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.
  • Mustafa Kemal
    Mustafa Kemal about 9 years
    Location can be under Properties>C/C++>General>Additional Include Directories.
  • Sonic
    Sonic over 8 years
    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?
  • Kit10
    Kit10 about 8 years
    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.
  • user2165
    user2165 over 7 years
    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.
  • ATL_DEV
    ATL_DEV about 7 years
    @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.
  • christopher clark
    christopher clark almost 5 years
    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!
  • yoyo
    yoyo about 4 years
    Nice and simple, but it's too bad the relative path is needed everywhere you include a header.
  • Chris Crozz
    Chris Crozz over 3 years
    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
  • Jim Beveridge
    Jim Beveridge over 2 years
    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.
  • Masoud Rahimi
    Masoud Rahimi over 2 years
    Defiantly this should be the accepted answer.