1

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?

4
  • Once you have defined a solution with the 2 projects P1 and P2, drag and-dropping the P1 files (except h1.h) from explorer into P2 should do the trick? or am i misunderstanding your question? Commented Oct 3, 2013 at 14:08
  • (appending to previous comment), and then adjusting for each project the include path to point to a specific h1.h Commented Oct 3, 2013 at 14:17
  • i have two project with identical files that i have set up. the problem is not creating the projects themselves. now in h1.h for P2 i want a different "#define" of "a" Commented Oct 3, 2013 at 14:46
  • Just a thought; maybe simply create hard-links so you have two names for the same file in multiple projects..? Commented Nov 6, 2023 at 16:03

3 Answers 3

1

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.

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

5 Comments

you see h1.h file for P1 and P2 are are exactly the same except for the #define. which potentially makes h1.h different for the two project. so let us call it h2.h. now in the cpp files of project1 i need to include h1.h. and the same cpp files of project2 needs h2.h.
Couldn't use use an #ifdef MYSYMBOL / #else / #endif construct to separate the small parts that are different for each project? Then define the symbol in one project, but not in the other. That would still allow you to share the header file.
I don't recommend using funny relative paths in #include directives. Use the -I option to gcc, or your compiler's project settings, to find the header files. If you use the hardcoded relative paths, you can not reorganize the structure of the source code directories.
@MarkLakata there are times when using include "../file.h" makes good sense, though. For example, we have an embedded system that has several different hardware front-ends, so the core system is in a folder with the core/common .c/.h files and each hardware platform project is in a subfolder that includes the core parts from above. That kind of thing will never be reorganized. But you're right, some types of projects are bound to get reorganized -- someday. The way VS buries include settings and changes them between editions is a bit of a PITA, hence my suggestion.
@SteveValliere - no matter what, source code trees always get lopsided with the passage of time and you will want to reorganize. Your embedded system might have arm and 8051 subfolders at the start, and then you suddenly realize it should have been arm\cortexm3 and arm\cortexm4 and arm\pxa320 and 8051 and intel\x64\linux and intel\x32\windows later in the game... It happens.
0

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.

2 Comments

can this work ? to pick up "h1.h" in the .cpp files i will have to change as p1_dir\h1.h or p2_dir\h1.h ?
Usually your project will search for *.h files given a well known search path. It first looks in the directory that contains the *.cpp file, then looks in the list of additional include directories. You would specify the additional include directories in your project settings or using the -I option to gcc, ie gcc -Ip1_dir ... and then use #include "h1.h"
0

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.

Comments

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.