user1612986
user1612986

Reputation: 1415

C++ using same file for different project

I have a C++ Visual Studio project (say p1) in which in one of the header file (say h1.h) I have

#define a 5

the project also has many other .cpp files many of which includes h1.h

Now I want to have a different independent project (say p2) in which the only change is in h1.h which is

#define a 6

I want to reuse all the files of p1 in p2. Question is what is a way I can have a design such that I have two such project in the same solution without duplicating too many files?

Upvotes: 1

Views: 1463

Answers (3)

user1612986
user1612986

Reputation: 1415

What I ended up doing is put the #define as a preprocessor directive in the project settings (just moved it out of the code header file) and that solved my problem.

Upvotes: 0

Steve Valliere
Steve Valliere

Reputation: 1207

Could you use a folder structure something like this for your projects:

MySolution\
  Common\
  Proj1\
  Proj2\

If you (or the IDE) don't like the 'Common' folder, you could also keep the common files in the 'MySolution" folder and have include statements like:

#include "../h1.h"

in your projects for the common files. You may also include shared code files from a common folder in multiple projects. When they are all in the same hierarchy, the IDE seems content, but it may complain (but still work) if they are in completely different hierarchies.

Upvotes: 1

Mark Lakata
Mark Lakata

Reputation: 20903

Put all of the common files in common_dir\*.cpp folder, put the header files in p1_dir\h1.h and p2_dir\h1.h.

Upvotes: 0

Related Questions