【发布时间】:2014-03-09 20:10:17
【问题描述】:
我有以下 .h 文件
#ifndef _EXCEPTION_H
#define _EXCEPTION_H
...
const char* _ErrorCode[] = {
"%s",
"Error: Index is out of range.",
"Error: Stack is empty.",
"Error: New matrix size is not greater than current one.",
"Error: Index or size cannot be negative.",
"Error: Method option is not within permissible options."
"Error: File could not be found or created."
"Error: There was not enough memory to allocate container."
}; // Error code list
...
#endif
包含在许多文件中。但是当我尝试编译时,出现以下链接器错误:
4 warnings generated.
duplicate symbol __ErrorCode in:
exception.o
graph.o
duplicate symbol __ErrorCode in:
exception.o
io.o
duplicate symbol __ErrorCode in:
exception.o
list.o
duplicate symbol __ErrorCode in:
exception.o
matrix.o
duplicate symbol __ErrorCode in:
exception.o
stack.o
这意味着编译器正在重新定义包含 exception.h 的所有文件中的 _ErrorCode 全局变量,即使文件开头有 #ifndef。我如何阻止这种情况发生?如果我不能,我如何定义一个只读字符串数组,以便在出现此类错误时打印?
【问题讨论】:
-
你需要在header(
extern)中声明,并且在一个.c文件中只定义一次
标签: c global-variables header-files