Header guard / translation unit problem

后端 未结 2 1019
长发绾君心
长发绾君心 2021-01-14 23:51

I was under the impression that header guards solve the problem of redefinition. I\'m getting linker errors that say there are redefinitions in the .obj files. This is the h

2条回答
  •  爱一瞬间的悲伤
    2021-01-15 00:44

    Header guards solve the problem of including the same header twice or hidden recursion of includes, not double definition.

    If you include the same header in different translation units, header guards won't help.

    The solution is to never declare variables in header files. If you need to share variables, use the extern keyword in the header, and declare the actual variables in one of the translation units.

提交回复
热议问题