由于某些原因,即使我使用标头警卫,我仍然在头文件中获得多个内容声明。我的示例代码如下:标题/包括警卫不工作?
的main.c:
#include "thing.h"
int main(){
printf("%d", increment());
return 0;
}
thing.c:
#include "thing.h"
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
int something = 0;
int increment();
#endif
当我尝试编译此,海湾合作委员会说,我有多个变量的定义。 ifndef应该确保这不会发生,所以我很困惑它为什么。
*编译器*错误“multiple * declarations *”与* linker *错误“multiple * definitions *”不一样。你在你的问题中提到过两个问题(实际上唯一的问题是后者);了解差异是了解发生了什么问题的关键。标头守卫防止多个*声明*,而不是多个*定义*。 – Clifford
@Clifford对不起,我应该提到我得到一个链接器错误。 – user1007968
另外,在C'int increment()中;'不是一个原型,而是声明了一个具有未指定数量参数的函数。为此,使用'int increment(void);'。 –