14

I'm compiling some code that relies on include guards to prevent multiple definitions of objects and functions, but Visual Studio 2008 is giving me link errors that there are multiple definitions. I don't understand why because I've used code very similar to this before and it hasn't caused problems. I must be doing something dumb but I have no idea what it is. I also tried to take out the include guards and use #pragma once, but I get the same link errors. What should I check for?

3 Answers 3

38

If they are linker errors, the most likely cause is probably non-inline functions defined in the header.

If you have a non-inline function in a header that is included in more than one source file, it will be defined in each of those source files ("translation units"), thus the function will be defined more than once, hence the multiple definitions error.

4
  • you can also make them weak symbols by making them templates
    – jk.
    Dec 9, 2009 at 10:39
  • 8 years later and this is still helpful.
    – iHowell
    Mar 16, 2018 at 16:31
  • 1
    10 years later and this is still helpful :D
    – GuillemVS
    May 1, 2020 at 17:35
  • 1
    for anyone reading this being stuck on dealing with this information: the simplest solution i could come up with is simply making them static
    – TK36
    Jan 1, 2022 at 21:51
1

If you're getting linker errors... are you sure you're not 1) actually defining a function twice in code or 2) trying to do something silly like #include a source file (as opposed to a header file)?

0

This can also be caused by using different versions of the cstd lib from other library's linked in. Check under the c++/Code generation section and make sure all your projects are using the same settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.