5

如何处理来自 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

cryptography.h

#ifndef CRYPTOGRAPHY_H
#define CRYPTOGRAPHY_H

#include <GenericTypeDefs.h>

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

#ifdef  __cplusplus
extern "C" {
#endif

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

显然,我没有重复#include <GenericTypeDefs.h>,并且checksum.ccryptography.ccrc8.c

4

3 回答 3

7

忽略它

如果你有包括警卫,这是一个虚假的警告,可以(应该)被忽略。我们使用包含守卫是因为允许多次包含一个文件是一种很好的做法,可以实现更灵活的代码,防止人为错误,并避免在您的#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 */ }

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

于 2011-09-26T14:41:30.637 回答
1

您可以使用以下选项仅针对该标头禁用警告:

-efile(537,GenericTypeDefs.h)

如果你有的话,把它添加到你的 Lint 配置中。

于 2011-09-26T14:05:34.183 回答
0

您链接到的文章解释说,这更像是 PCLint 的怪事,应该是注释而不是警告。

只需忽略它/禁用它。

于 2011-09-26T14:12:11.657 回答