假设我有三个文件:
//m.h const int RED = 1; //m.h ends here //f1.c #include "m.h" //f1.c ends here //f2.c #include "m.h" int main() {return 0;} //f2.c ends here
单独编译每个将工作,但gcc -Wall f1.o f2.o -o prog
将产生:
multiple definition of 'RED'
现在,如果我用以下内容替换const:
//m.h enum {RED=1} colors; //m.h ends here
我将能够编译prog
并RED
用作const,不会出现任何 multiple definition
错误.
enum
当您在不同文件中使用具有相同名称的全局变量或结构时,为什么s 的行为与可见的行为不同?