如何包含特定的头文件而不会发生冲突

Kra*_*ken 1 c compilation header

我的档案是

main.c

#include"A.h"
#include"B.h"
Run Code Online (Sandbox Code Playgroud)

A.c

#include"A.h"
Run Code Online (Sandbox Code Playgroud)

B.c

#include"B.h"
Run Code Online (Sandbox Code Playgroud)

我有一个文件,我已经定义了几个结构,我应该在所有文件中使用A.c , B.c, main.c,甚至是头文件A and B.

因此,我有

A.h并且B.h都有

#include"struct.h"
Run Code Online (Sandbox Code Playgroud)

现在,我在main.c中看到了

我将对这两个结构进行多次声明,如何摆脱这个问题.我的结构会改变什么?

谢谢

svk*_*svk 7

使用包括警卫.

aheader.h:

#ifndef AHEADER_H
#define AHEADER_H

// ... rest of header here

#endif
Run Code Online (Sandbox Code Playgroud)

bheader.h:

#ifndef BHEADER_H
#define BHEADER_H

// ... rest of header here

#endif
Run Code Online (Sandbox Code Playgroud)

  • @JoachimPileborg我应该为每个文件执行此操作(我想我将确保没有任何声明重复),但如果我接受我的情况,那么我想只有struct.h应该有这些警卫,对吧?既然这是唯一容易重复的声明? (2认同)