忽略它
如果你有包括警卫,这是一个虚假的警告,可以(应该)被忽略。我们使用包含守卫是因为允许多次包含一个文件是一种很好的做法,可以实现更灵活的代码,防止人为错误,并避免在您的#include
语句中具有顺序重要性。如果您想知道为什么会这样,请继续阅读。
多个包含不是问题,因为您有包含警卫。
由于这些 .h 文件都非常相关,我猜您将所有三个文件都包含在同一个文件中的某个地方,也许main.c
:
#include "checksum.h"
#include "cryptography.h"
#include "crc8.h"
main () { /* Do Stuff */ }
这将扩展(当然,减去评论)为:
// CHECKSUM_H is not defined, so the preprocessor inserts:
#include <GenericTypeDefs.h>
BOOL isCheckSumCorrect(const UINT8 *dataPointer, UINT8 len, UINT8 checkSumByte);
// CRYPTOGRAPHY_H is not defined, so the preprocessor inserts:
#include <GenericTypeDefs.h>
UINT8 encrypt(UINT8 c);
UINT8 decrypt(UINT8 c);
// CRC_H is not defined, so the preprocessor inserts:
#include <GenericTypeDefs.h>
UINT8 generateCRC(const UINT8 *ptr, UINT8 Len);
BOOL isCRCValid(const UINT8 *ptr, UINT8 Len, UINT8 CRCChar);
main () { /* Do Stuff */ }
所以是的,#include <GenericTypeDefs.h>
在预处理器步骤的某个时刻确实出现了多次。从表面上看,该文件看起来像:
#ifndef GENERIC_TYPE_DEFS_H
#define GENERIC_TYPE_DEFS_H
#define UINT8 unsigned char
#ifdef __cplusplus
extern "C" {
#endif
因此,经过更多预处理后,我们之前扩展的位(减去注释)变为:
// GENERIC_TYPE_DEFS_H is not defined, so the preprocessor inserts:
#define UINT8 unsigned char
BOOL isCheckSumCorrect(const UINT8 *dataPointer, UINT8 len, UINT8 checkSumByte);
// GENERIC_TYPE_DEFS_H IS defined, so the preprocessor inserts nothing.
UINT8 encrypt(UINT8 c);
UINT8 decrypt(UINT8 c);
// GENERIC_TYPE_DEFS_H IS defined, so the preprocessor inserts nothing.
UINT8 generateCRC(const UINT8 *ptr, UINT8 Len);
BOOL isCRCValid(const UINT8 *ptr, UINT8 Len, UINT8 CRCChar);
main () { /* Do Stuff */ }
进一步的预处理(未显示)复制#define
整个代码。
如果您注意其他预处理器和 linter 警告,则可以保证包含警卫
如果任何.h
文件缺少标准包含保护,linter 会警告您(错误 451 和 967)。GenericTypeDefs.h
如果multiply-included没有包含保护,编译器将警告重复的符号定义。如果没有,请创建您自己MyGenericTypeDefs.h
的头文件或切换<stdint.h>
到头文件,它是 C 标准的一部分,并提供与您的GenericTypeDefs.h
.
警告:前面有不好的解决方法
如果您真的坚持修复警告而不是忽略它,则必须#include <GenericTypeDefs.h>
从每个.h
文件中删除并输入一次,然后才能包含一个或多个这些文件,如下所示:
checksum.c
#include <GenericTypeDefs.h>
#include "checksum.h"
cryptography.c
#include <GenericTypeDefs.h>
#include "cryptography.h"
crc.c
#include <GenericTypeDefs.h>
#include "crc.h"
main.c
#include <GenericTypeDefs.h>
#include "checksum.h"
#include "cryptography.h"
#include "crc8.h"
main () { /* Do Stuff */ }
这是不推荐的,因为它会给你带来更多的工作和更多的出错机会(没有冒犯,但你只是人类,预处理器不是)。相反,只需要求预处理器完成其工作并按照设计的方式使用包含防护。