0

Possible Duplicate:
Is it possible to #Include in a “diamond heritage” stracture?

I'm building some program who looks like that:

"a.c" #includes "a.h" and #includes "b.h"
"b.c" #includes "b.h"

lets say I declare on a variable only in "b.h", named "var"

While tying to compile that on "cmd" with "nmake" I get the following error:

b.obj : error LNK2005: var already defined in a.obj

But I didn't define it on a.c or a.h, Only on b.h

What can cause this? thanks

Community
  • 1
  • 1
hudac
  • 2,584
  • 6
  • 34
  • 57
  • Wasn't that answered [here](http://stackoverflow.com/questions/13937523/is-it-possible-to-include-in-a-diamond-heritage-stracture) already? – Daniel Fischer Dec 18 '12 at 17:30
  • You mention in the other post that you are already using include guards, can you post them here? – Hunter McMillen Dec 18 '12 at 17:32
  • This was asked yesterday by a different poster - http://stackoverflow.com/questions/13918807/how-to-troubleshoot-through-gcc-temporary-file-creation/13919464#13919464 – user93353 Dec 18 '12 at 17:33
  • @HunterMcMillen, include guards won't help if multiple `.c` files include the header with the variable definition as there will be multiple `.o` files being linked to construct the final binary. Each `.o` having a definition of the variable. – hmjd Dec 18 '12 at 17:33
  • thanks, yeah, its the extern thing – hudac Dec 18 '12 at 17:37

1 Answers1

1

But I didn't define it on a.c or a.h, Only on b.h

That's not how it works. All code from all included headers is put together by the preprocessor and passed to the compiler, which then compiles it into an object file. This code is called a "translation unit." If the same variable is defined in another translation unit, it will also be present in the generated object file. When you then link these object files together, both will have the same variable defined, and thus the linker will complain about the duplicate symbol.

What you should do instead is define the variable in one *.c file, and only declare it extern in the *.h file. That way, the other translation units will know about the variable but will not try to define it themselves. It will only be defined in the translation unit that includes the *.c file where it's actually defined.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • "If the same variable is defined in another translation unit" why would the file be defined in another translation unit ? – hudac Dec 18 '12 at 17:50
  • @hudac Because it includes the \*.h file where the variable is defined. That means the definition will be part of that translation unit. If all translation units include that \*.h file, then the variable will be defined in all of them. The `extern` keyword allows you to *declare* the variable in all translation units, but only define it in one. – Nikos C. Dec 18 '12 at 17:55
  • "If all translation units include that *.h file, then the variable will be defined in all of them".. How much translation units there are ? – hudac Dec 18 '12 at 18:08