WRONG
const char * MUTEX_NAME = "mutex_shm";
const char * FULL_NAME = "full_shm";
const char * PATH_NAME = "./text.txt";
ERROR
cc -o edit init.o common.o
common.o:(.data+0x0): multiple definition of `MUTEX_NAME'
init.o:(.data+0x0): first defined here
common.o:(.data+0x8): multiple definition of `FULL_NAME'
init.o:(.data+0x8): first defined here
common.o:(.data+0x10): multiple definition of `PATH_NAME'
init.o:(.data+0x10): first defined here
标准C中const定义的变量是外连接的,即如果一个编译单元中定义了一个全局const常量,则其在其他编译单元中是可见的,如果其他编译单元也定义了同名const常量就会产生重复定义错误。这一点与C++不同,C++中const定义的变量是内连接的,即每个编译单元定义的全局const常量是自己独有的。
RIGHT
static const char * MUTEX_NAME = "mutex_shm";
static const char * FULL_NAME = "full_shm";
static const char * PATH_NAME = "./text.txt";
网友评论