0

Background: I'm working on a legacy project in C++. While extending the functionality of a base class(specifically I'm adding multi-threading support), I had to modify a header file. Let's call the base class header fooBuilder.h . fooBuilder.h defines a builder object that is used heavily later in the code. To implement multi-threading, I included a bunch of windows specific headers.

Problem: In the other source files that include fooBuilder.h(among other header files), I get "error C2371: '[windows variable name]' : redefinition; " errors during compilation. Obviously, if I could change EVERY source file in my project to include fooBuilder.h as the first header file I'm good, but there are a 101 files which include fooBuilder.h! Abstracting out the windows specific headers into stdafx.h is not an option. What is the best way to overcome this situation? I'm open to any and all suggestions. I really don't want to change the "#include fooBuilder.h" position for every file!

Prashanth
  • 3
  • 1
  • 5

1 Answers1

0

Seeing fooBuilder.h + the exact change you did in it would help figuring out what the best way to fix this is.

Here are two possible solutions, hoping one will fit your needs:

1- Move the windows include from fooBuilder.h to fooBuilder.cpp. In almost every case it is doable. Whatever attribute you added to the class can be moved to a pointer (then use forward declaration), whatever parameter you added to a method can be passed by pointer or const reference (then use forward declaration too), whatever type you used can be redefined (like if you are using PVOID, just declare it yourself typedef void *LPVOID;). This is definitely the best and nicest way to fix your problem.

2- A windows header most likely ends up being included twice (one by the .cpp file causing the problem when you compile and one by fooBuilder.h). You can maybe prevent this from fooBuilder.h. Check where the variable is declared and check if you can prevent it's declaration through a pre-processing variable (or, even better, prevent the whole file from being included). Note that changing the order of the includes won't fix that.

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Forward declaration was indeed helpful. I thought about it as soon as I posted the question. I do have a question regarding point 2 though: Wouldn't my header guard prevent that from happening? Thanks for the answer! – Prashanth Oct 23 '15 at 20:24
  • Your header guard prevents `fooBuilder.h` being included once, but the windows file you include may not have the guard and that's why you get a variable declared twice. – jpo38 Oct 23 '15 at 20:54
  • By the way, if this fixes your issue, please vote up (additionnaly to accept the answer, what you already did: those are two different operations). Thanks. – jpo38 Oct 23 '15 at 20:56
  • Ah I see what you meant. I had verified that the windows headers had header guards :). I tried to upvote, but since I don't have enough reputation, it's not shown publicly – Prashanth Oct 23 '15 at 21:26