Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I share C++ source code files between projects in Visual Studio?

I'm writing a cross-platform application. One version will work under Win32 and the second on Windows Phone.

I'd like to reuse my C++ core - especially that there are no platform dependencies inside, STL only. I order to do so, I want to use the same source files in two projects: static Win32 library (.lib) and Windows Phone Component (C++/CLI).

How can I configure these two projects to use exactly the same source and header files?


OK, let's take an example. Let's say, that I have project:

MyApp.Library [win32 lib]
    myClass.cpp
    myClass.h

This library is compiled to .DLL file and then imported in a Win32 application:

MyApp.Win32App [win32 C#]

Since Win32 is not compatible with Windows Phone on the binary level, I cannot use that library directly. But since the library uses only STL, I can create a Windows Phone component, put all its sources there, and build.

MyApp.Component [Windows Phone component]
    myClass.cpp
    myClass.h

I want these two files to be exactly the same ones as used in the library. How should I organize the project to achieve this effect?

like image 627
Spook Avatar asked Sep 22 '14 18:09

Spook


People also ask

How do I share code between projects in Visual Studio?

If you select Start Collaboration session from the Session Details menu, an invitation link to your session will automatically be copied to your clipboard. You can share this link with anyone you'd like to collaborate with, as long as they also have VS Code and the Live Share Extension Pack downloaded.

How do I link two projects in Visual Studio?

Right-click on project 2 in the solution explorer and select "Properties". In the C/C++ section select "General". Find the line "Additional Include Directories" and add "$(SolutionDir)project 1". This is a semicolon separated list but you can as well choose to edit this field, then you get a list view.

What is a shared items project in Visual Studio?

You can think of “shared items” projects as another way of organizing your files in case you have many projects building the same source file (e.g. in case you build multiple applications that share the same source and you're not already using static libraries for this purpose).

How do I add a project from another solution in Visual Studio?

In Solution Explorer, select the solution. On the File menu, point to Add, and click Existing Project. In the Add Existing Project dialog box, locate the project you want to add, select the project file, and then click Open. The project is added to the selected solution.


2 Answers

You can add source code from a common location to multiple projects. I do that a lot; I have code in a common directory that is at the same level in the directory hierarchy as most of my project files. It's simply a matter of adding .h and .cpp files from the common directory to your various projects.

I did notice that VisualStudio gets a little cranky and difficult if you use a network drive for common source, so I don't do that. But so long as all of your source code is on local disks, and the IDE knows where to find them, there is no problem having the same source file in very many projects.

like image 110
Logicrat Avatar answered Sep 18 '22 20:09

Logicrat


one way is to put all .hpp and .cpp files into a seperate folder - call it "Shared".

Then add an additional include Directory inside your solution configuration property - this directory must be a relative path.

Then add ONLY the .cpp files relative from that shared folder into your project

NB! If you have include "stdafx.h" inside your .cpp files in the Shared folder, then comment those out.

like image 40
serup Avatar answered Sep 21 '22 20:09

serup