出于某种原因,我在头文件中获得了多个内容声明,即使我正在使用标题保护.我的示例代码如下:
main.c中:
#include "thing.h"
int main(){
printf("%d", increment());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
thing.c:
#include "thing.h"
int increment(){
return something++;
}
Run Code Online (Sandbox Code Playgroud)
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
int something = 0;
int increment();
#endif
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,GCC说我对some变量有多个定义.ifndef应该确保不会发生这种情况,所以我很困惑为什么会这样.