开发者

extern variable - why?

I've heard that you shouldn't def开发者_如何学JAVAine anything in header files, because of the possibility of multiple defines, but if you have include guards, this shouldn't happen, right? What other reasons are there for adding extern to variables?


Include guards merely prevent multiple inclusion of a header within a single translation unit (aka compilation unit). This does not address the problem of multiple definitions from separate translation units at link time. Hence you should only ever put declarations in header (.h) files, and definitions in source (.c) files.


You generally declare extern variables in header files when the variable is defined in one source file (more specifically, one translation unit), and referenced in another.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜