visual c++: #include files from other projects in the same solution
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.
Visual Studio: Include .h or .cpp in two-project solution
It's normal. If you have 2 projects, 2 binaries will be generated.
Don't include the cpp file.
Instead, link the binaries together.
main project - generates .lib
file and either .dll
or .exe
.
test project - includes header from main
. You need to add the .lib
generated by main in the additional dependencies of the test project. Somewhere in the Project Settings - Linker Options - Additional Dependencies.
You can generate both .exe and .lib file from a single project. To do this you set:
- exe in
Linker -> General -> Output File
- lib in
Linker -> Advanced -> Import Library
You may also need to mark exported functions with __declspec( dllexport )
in the .exe project (see docs), otherwise compiler won't generate a .lib file.
Related Topics
What Does the Operation C=A+++B Mean
Linking a Shared Library with Another Shared Lib in Linux
Are Inner Classes in C++ Automatically Friends
Clarification Needed Regarding Getchar() and Newline
Passing Arguments to Std::Async by Reference Fails
Serial Comm Using Writefile/Readfile
How to Run a Child Process That Requires Elevation and Wait
C++ Inline Member Function in .Cpp File
How to Embed a File into an Executable
Opencv Error: Assertion Failed (Size.Width>0 && Size.Height>0) Simple Code
About Binding a Const Reference to a Sub-Object of a Temporary
#Define Nominmax Using Std::Min/Max
Ampersand (&) at the End of Variable etc