John
John

Reputation: 6278

Visual Studio Include cpp file from other project

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?

Upvotes: 2

Views: 22913

Answers (5)

Mujin
Mujin

Reputation: 39

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.

Upvotes: 0

Brandon Kohn
Brandon Kohn

Reputation: 1621

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.

Upvotes: 0

Vasilly.Prokopyev
Vasilly.Prokopyev

Reputation: 874

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.

Upvotes: 5

MSalters
MSalters

Reputation: 180305

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.

Upvotes: 0

spyym
spyym

Reputation: 9

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

Upvotes: -1

Related Questions