Search code examples
c++visual-studioversion-controlprojects-and-solutions

Visual Studio "synchronize" multiple projects within one solution



I am writing my own C++ project library in visual studio (couple of projects that consist of *cpp, *.h files, NOT an actual .lib file). Each of these projects is located in single Visual Studio "solution".

When I needed to use a code from one project in another, I just copied it. But that was short term solution. Now my library has grown both in size and functionality, and because of code duplication I ended up with dozens of different versions of the same file.

That single solution serve me as a playground // test ground for new ideas. I am developing projects not libs. Currently it holds about 50 projects.

I am working on visual studio 2015.


Lets say I have setup like this:

  1. DataIO project located in */Solution/#DataInputAndOutput/DataIO/DataIO.h
    consist both of DataIO.h and DataIO.cpp
  2. Foo project located in */Solution/#Foo/Foo/Foo.h
    consist both of Foo.h and Foo.cpp

DataIO.h:

#pragma once
#ifndef __DATA_IO_H__
#define __DATA_IO_H__
// ...

extern FILE * const logFile;

// Bunch of function declarations
#endif // !__DATA_IO_H__

I know that this is not a "Minimal, Complete, and Verifiable example" but my problem lies in logistic of things, not things themselves. And I believe that my description is sufficient.


Problem #1: In Foo.cpp I #include "DataIO.h" (with is possible because i added additional include directories in Foo project setup) But whenever I try to compile Foo I am given following error: unresolved external symbol for every function declaration inside DataIO.h and that one external variable. How Can I Fix this problem? Do I need to create a DataIO.lib to keep things straight?

I tried to add DataIO.h and DataIO.cpp into Foo project directly (NOT copy it, just add it into project) but that seems like a bad idea...


Solution

  • I recommend trying out the new "Shared Items Project" in VS2015+. A Shared Items Project is literally just a set of files. The project itself doesn't build anything. In fact, there are no (or almost no) compilation or linkage settings in a Shared Items Project -- those settings come the the project that references the Shared Items Project. When you reference a Shared Items Project from some other project FOO, the build of FOO happens as if the files of the Shared Items Project are items of the referencing project.

    Short of that, you can set up your shared projects to build .libs and then use Project References within Visual Studio to automatically set up the linkage. I think you have to manually set up the include paths when doing this, though.