碰到个问题折腾了半天,是C和C++中全局变量以及全局const变量的链接特性和可见性的问题,先摘录一段C++编程思想里的文字:
In C++, a const doesn't necessarily create storage. In C a const always creates storage. Wheater or not storage is reserved for a const in C++ depends on how it is used. In general, if a const is used simply to replace a name with a value(just as you would use a #define), then storage doesn't have to be created for the const. If no storage is created(this depends on the complexity of the data type and the sophistication of the compliler), the values may be folded into the code for greater efficiency after type checking, not before, as with #define. If, however, you take an address of a const(even unknowingly, by passing it to a function that takes a reference argument) or you define it as extern, then the storage is created for the const.
C中,比如a.c和b.c中,如果两个文件同时出现同名的全局变量且并没有在两个文件里都初始化时,是可以编译通过的,但请不要那么写。好&
In C++, a const doesn't necessarily create storage. In C a const always creates storage. Wheater or not storage is reserved for a const in C++ depends on how it is used. In general, if a const is used simply to replace a name with a value(just as you would use a #define), then storage doesn't have to be created for the const. If no storage is created(this depends on the complexity of the data type and the sophistication of the compliler), the values may be folded into the code for greater efficiency after type checking, not before, as with #define. If, however, you take an address of a const(even unknowingly, by passing it to a function that takes a reference argument) or you define it as extern, then the storage is created for the const.
C中,比如a.c和b.c中,如果两个文件同时出现同名的全局变量且并没有在两个文件里都初始化时,是可以编译通过的,但请不要那么写。好&