小编use*_*968的帖子

标题/包括警卫不起作用?

出于某种原因,我在头文件中获得了多个内容声明,即使我正在使用标题保护.我的示例代码如下:

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应该确保不会发生这种情况,所以我很困惑为什么会这样.

c header include-guards

5
推荐指数
1
解决办法
6622
查看次数

标签 统计

c ×1

header ×1

include-guards ×1