Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 成功编译后出现链接器错误_C_Linker Errors - Fatal编程技术网

C 成功编译后出现链接器错误

C 成功编译后出现链接器错误,c,linker-errors,C,Linker Errors,我有以下(伪)代码: file.h extern int a; extern int b; extern int *c; // function declarations /*use `a`,`b` and` c` for some operation*/ /*use `a`,`b` and `c` for some operation*/ /* does not use `a`, `b` or `c`*/ file2.c——包含file.h extern int a; extern i

我有以下(伪)代码:

file.h

extern int a;
extern int b;
extern int *c;
// function declarations
/*use `a`,`b` and` c` for some operation*/
/*use `a`,`b` and `c` for some operation*/
/* does not use `a`, `b` or `c`*/
file2.c
——包含
file.h

extern int a;
extern int b;
extern int *c;
// function declarations
/*use `a`,`b` and` c` for some operation*/
/*use `a`,`b` and `c` for some operation*/
/* does not use `a`, `b` or `c`*/
file3.c
——包含
file.h

extern int a;
extern int b;
extern int *c;
// function declarations
/*use `a`,`b` and` c` for some operation*/
/*use `a`,`b` and `c` for some operation*/
/* does not use `a`, `b` or `c`*/
file4.c
——包含
file.h

extern int a;
extern int b;
extern int *c;
// function declarations
/*use `a`,`b` and` c` for some operation*/
/*use `a`,`b` and `c` for some operation*/
/* does not use `a`, `b` or `c`*/
变量被声明为
extern
,因为相同的变量必须在多个文件中使用。 现在,所有文件的编译都成功了,但我收到一个链接器错误,在
file2.c
file4.c
以及
file3.c
file4.c
中显示了
a
b
file4.c
的多个定义:

Error-
unresolved extern a in file4.c
unresolved extern b in file4.c
unresolved extern c in file4.c
multiple definition  of a in file4.c and file2.c
multiple definition  of a in file4.c and file3.c
multiple definition  of b in file4.c and file2.c
multiple definition  of b in file4.c and file3.c
multiple definition  of c in file4.c and file2.c
multiple definition  of c in file4.c and file3.c
由于我没有在
file4.c
中使用任何变量,而是只使用
file.h
中定义的函数声明

问题:
错误的原因可能是什么?

您可以声明
extern[type][variable name].h文件中编码>任意次数,甚至在同一个.h文件中多次编码。(这是毫无意义的,但没有什么能阻止你这么做。)

然而,为了即使是一个
extern[type][variable name]
要在没有链接器错误的情况下工作,您还必须有一个普通的
[type][variable name](没有外部代码)

  • 如果您没有普通的
    [type][variable name]
    在某些文件中,会出现“未解析外部”链接器错误

  • 如果您有多个
    [type][variable name]
    在不同的文件中,会出现“多定义”链接器错误

所以,最有可能发生的是,您缺少了一个普通的
inta当你有多个
intb
intc某处。
int b
int c
的这些错误重复可能在不同的.c文件中,或者甚至可能在单个.h文件中,因为每次.h文件包含在.c文件中时,它都会起作用

因此,最好的方法是声明
inta
在file.h头文件中,以及在您希望使用变量
a
的每个其他文件中,将其声明为
extern int a


通常,最好的方法是声明
extern[type][variable name].c文件所包含的.h文件中的code>,以及
[type][variable name]仅在一个.c文件中。

您是否在某处定义了
a、b、c
?定义可以这样做:
inta,b,*c嗯,这是我的错。问题不是没有定义,而是多个定义。你得到的确切的逐字错误是什么。编辑你的答案并添加它!变量仅在文件.h中声明,该文件包含在每个源文件中,变量定义在哪里?答案为thnaks。我对c比较陌生。我没有在没有extern的情况下声明a、b和c。我应该在哪里声明变量而不使用extern,因为我有两个文件访问同一个变量。此外,变量不在任何其他地方声明,无论是否有外部变量在一个源文件中(并且只有一个源文件)变量的实际声明我不在任何c文件中声明变量,我直接在源文件中使用它们,即给它赋值,在这种情况下,更改后的变量值可以在其他文件中访问如果您没有在任何c文件中声明变量,则会出现链接器错误。所以,你必须先解决这个问题。是的,一旦您在一个c文件中声明了每个变量,那么它们的值就可以通过
extern
声明从其他文件中看到。