2

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?

3
  • looks like you have a problem with relative paths. can you check from the project's vcxproj path if "..\..\..\..\..\..\my project\myfile.cpp" exist? Commented Jul 28, 2014 at 9:19
  • @NirMH I've even tried absolute paths and it gives the same issue. Commented Jul 28, 2014 at 10:01
  • I'm using symlinks in windows and it works fine Commented Feb 9 at 19:56

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

add 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".
@zwcloud Actually - at least in VS 2022 - the file is linked, not copied. So, works perfectly. Working with C# projects on a daily basis, this isn't intuitive since the add button looks differently when adding an existing item to a C# project.
0

You can't just pick files like that. There are two reasonable ways to solve it. 1, shared the file by means of a Code Versioning System (e.g. svn/git). 2, compile the the .cpp into a library and link to that library.

Comments

0

If the cpp can be used by multiple projects, it must mean that the code is something common. That means you should compile that code by itself into a library and then share that library. Compiling the same cpp into multiple libraries is likely to result in conflicts later if two such libraries are ever needed to work together.

Comments

0

In my case (for VS2022), I had the same errors when I wanted to transfer code from one project to another. Instead of drag n drop folders/files from one project to other, the way it worked for me is by manually creating the folders and files and inserting in there the code as text with copy-paste.

1 Comment

The files should also be added to the makefile; otherwise, it won't work.
-1

Try to drag them into your solution? You can create a new folder in your solution, and drag them all into this folder!

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.