ваша проблема сводится к пониманию классов хранения C ++; статический и внешний.
extern says: this variable is defined in a different file, this
is only a declaration
static says: this variable's lifetime is the program lifetime and
restrict it's visibility to the current file.
What happens in your case:
- each file includes the variable declaration and definition `int my_var = 100;`,
so both object scr1.o and scr2.o will contain the variable definition. When
linking them, the linker cannot solve the ambiguity and throws an error.
- adding the extern qualifier and defining the variable in one of the files
solves this problem: in one source it'll be an external unresolved at compile
time, symbol and the other will contain the actual symbol.
Что касается второй части вашего вопроса; почему добавление статики, похоже, помогает компоновщику. Посмотрите, что я только что сказал о статическом классе хранения: в основном каждый объект будет иметь свою собственную копию переменной - не то же самое!