External variable - why?

I heard that you should not define anything in the header files because of the possibility of multiple definitions, but if you have defenders, this should not happen, right? What other reasons to add external variables?

+6
source share
2 answers

Turning on safeguards simply prohibits the multiple inclusion of a heading within a single translation unit (aka compilation unit). This does not address the issue of multiple definitions from individual translation units during communication. Therefore, you should only add declarations to header files (.h) and definitions in source files (.c).

+12
source

You usually declare extern variables in header files when a variable is defined in one source file (more specifically, one translation unit) and refers to another.

+1
source

Source: https://habr.com/ru/post/888856/


All Articles