What is the correct way of using extern for global variables?
问题 file a.cc int a = 0; file b.cc #include "a.cc" file main.cc #include "b.cc" extern int a; int main() { } g++ -c a.cc g++ -c b.cc g++ main.cc a.o b.o error: multiple definitions of a What am I doing wrong here? 回答1: You include a .cc (or .cpp ) files, which is wrong. Do not do that. You need a header, and in that put the extern int a; : // a.h // include guards omitted extern int a; // a.cc #include "a.h" int a; // b.cc #include "a.h" // main.cc #include "a.h" int main(){ // use a } 回答2: You