Visual Studio Include cpp file from other project
For cpp files you can just use right mouse click on project, select "add"->existing item. Then they should compile with others, when a build initiated.
Slightly more complicated with headers. There is two ways to use #include directive: with < > and " " braces
When " " braces used, compiler treats path as relative (if not absolute used) to location of cpp file.
When < > braces used, compiler looks for file in something like system include folders. For example - stdlib headers folder and windows.h location folder. Properties entry Additional Include Directories also goes there.
I suggest you to change projects structure and extract shared features from both projects to compile it as static library. Place shared headers in some subfolder inside library project and refer then as
#include "mylibHeaderDir/someheader.h"
In dependent projects, after setting Additional Include Directories you can refer theese includes as
#include <myLibHeaderDir/someheader.h>
This approach will help you in future, as you can reuse that shared module in every project you want.
About how to create and link static library you can read this article http://msdn.microsoft.com/en-us/library/vstudio/ms235627(v=vs.110).aspx Version of visual studio may be differ, but basics are the same.
John
Updated on June 13, 2022Comments
-
John almost 2 years
I have a C++ .h and .cpp file from another project that I want to include into my project. I don't want to copy the files over into my project since I want any changes to those files be applied to both projects.
I've included the directory of the file's folder in the
Properties->VC++ Directories->Include Directories
I've also included the folder in the
Properties->C/C++ -> General -> Additional Include Directories
The .h files seem to work. If I rename the include to anything other than
#include "myfile.h"
The cpp file gets unknown definitions.
When I compile. The error is
fatal error C1083: Cannot open source file: '..\..\..\..\..\..\my project\myfile.cpp': No such file or directory
If I remove the cpp file from the project. Then I get a long list of unresolved functions.
error LNK2019: unresolved external symbol "public: unsigned long __thiscall myclass::myfunction"
How can I include both the .h and .cpp file into my second project?
-
Bernd Wechner about 5 yearsThe question was included a clear "I don't want to copy the files over into my project since I want any changes to those files be applied to both projects." which this proposed solution violates.
-
zwcloud almost 5 yearsadd existing item will copy the file into the project directory. So that's not expected: asker said "I don't want to copy the files over into my project".