PC Lint警告537:重复包含文件

Dan*_*llo 4 c lint pc-lint

如何从PC Lint处理此警告?

我有几个文件#include <GenericTypeDefs.h>.PC Lint向我显示消息Warning 537: Repeated include file 'filepath\filename.h'如果我删除此声明我无法编译.

如果可能的话,我想取消这个警告.

你可以在这里看到同样的报道.

这是我的代码,我的编译器发出警告:

checksum.h

#ifndef CHECKSUM_H
#define CHECKSUM_H

#include <GenericTypeDefs.h>

BOOL isCheckSumCorrect(const UINT8 *dataPointer, UINT8 len, UINT8 checkSumByte);

#ifdef  __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)

cryptography.h

#ifndef CRYPTOGRAPHY_H
#define CRYPTOGRAPHY_H

#include <GenericTypeDefs.h>

UINT8 encrypt(UINT8 c);
UINT8 decrypt(UINT8 c);

#ifdef  __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)

crc8.h

#ifndef CRC_H
#define CRC_H

#include <GenericTypeDefs.h>

UINT8 generateCRC(const UINT8 *ptr, UINT8 Len);
BOOL isCRCValid(const UINT8 *ptr, UINT8 Len, UINT8 CRCChar);

#ifdef  __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)

很显然,我没有重复#include <GenericTypeDefs.h>checksum.c,cryptography.ccrc8.c

Kev*_*eer 6

只是忽略它

如果你有包含警卫,这是一个虚假的警告,可以(应该)被忽略.我们使用包含保护,因为允许多次包含文件是一种很好的做法,可以实现更灵活的代码,防止人为错误,并避免在#include语句中具有顺序重要性.如果你想知道为什么会这样,请继续阅读.

多个包含不是问题因为你有包含警卫.

由于这些.h文件都非常相关,我猜你将所有三个文件包含在同一个文件中,或许main.c:

#include "checksum.h"
#include "cryptography.h"
#include "crc8.h"

main () { /* Do Stuff */ }
Run Code Online (Sandbox Code Playgroud)

这将扩展(当然减去评论):

// 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 */ }
Run Code Online (Sandbox Code Playgroud)

所以是的,#include <GenericTypeDefs.h>确实在预处理器步骤中的某个时刻出现了多次.表面上看,该文件看起来像:

#ifndef GENERIC_TYPE_DEFS_H
#define GENERIC_TYPE_DEFS_H

#define UINT8 unsigned char

#ifdef  __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)

所以,经过更多的预处理后,我们之前扩展的位(减去注释)变为:

// 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 */ }
Run Code Online (Sandbox Code Playgroud)

进一步的预处理(未示出)复制#define整个代码.

如果你注意到其他预处理器和linter警告,你可以保证包括警卫

如果任何.h文件缺少标准包含警卫,则linter将警告您(错误451和967).如果multiply-included GenericTypeDefs.h没有包含保护,编译器将警告重复的符号定义.如果没有,请创建自己的MyGenericTypeDefs.h或切换到<stdint.h>标题,这是C标准的一部分,并提供与您的标准类似的功能GenericTypeDefs.h.

警告:未来的解决方法不好

如果您真的坚持修复警告而不是忽略它,则必须#include <GenericTypeDefs.h>从每个.h文件中删除并在包含一个或多个这些文件之前输入一次,如下所示:

checksum.c

#include <GenericTypeDefs.h>
#include "checksum.h"
Run Code Online (Sandbox Code Playgroud)

cryptography.c

#include <GenericTypeDefs.h>
#include "cryptography.h"
Run Code Online (Sandbox Code Playgroud)

crc.c

#include <GenericTypeDefs.h>
#include "crc.h"
Run Code Online (Sandbox Code Playgroud)

main.c

#include <GenericTypeDefs.h>
#include "checksum.h"
#include "cryptography.h"
#include "crc8.h"

main () { /* Do Stuff */ }
Run Code Online (Sandbox Code Playgroud)

这是不推荐的,因为它会为你带来更多的工作,并且你有更多的机会犯错误(没有冒犯,但你只是人类而预处理器不是).相反,只要求预处理器完成其工作并按照设计方式使用包含保护.