Why does Borland compile with multiple definitions of same object in different C files while GCC does not?

后端 未结 1 992
广开言路
广开言路 2021-01-19 04:26

I am studying the behavior of global variables.

So far , I thought the multiple definition of global variables is an illegal way , and must get an error. But I got

相关标签:
1条回答
  • 2021-01-19 04:38

    Multiple external definitions of an object is undefined behavior in C. A common extension is to accept multiple definitions if they don't disagree (usually with same type and no initialization value).

    C99 6.9p5 says:

    If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one"

    and C99, 4.p2:

    violation of a "shall" outside of a constraint implies UB

    0 讨论(0)
提交回复
热议问题