【问题标题】:PC Lint Warning 537: Repeated include filePC Lint 警告 537:重复包含文件
【发布时间】:2011-11-25 04:49:30
【问题描述】:

如何处理来自 PC Lint 的警告?

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

如果可能,我想取消此警告。

您可以看到相同的报告here

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

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

显然,我没有在checksum.ccryptography.ccrc8.c 上重复#include &lt;GenericTypeDefs.h&gt;

【问题讨论】:

  • 显示代码,没有它试图回答是没有用的。
  • @Griwes,我已经链接了一个关于相同问题的示例。
  • 您是否尝试过按照帖子中的建议进行操作?在 #include 文件之前检查标头保护,即在 crc.h 中,您将执行 #ifndef generictypes_h #include #endif。这是重复的努力,但它可能是唯一的方法。
  • 在计算世界的背景下,我的大脑无法将任何“爱丽丝”想象成一个真实的人。
  • @TomalakGeret'kal 你是对的。我删除此信息

标签: c lint pc-lint


【解决方案1】:

忽略它

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

多个包含不是问题,因为您有包含保护。

由于这些 .h 文件都非常相关,我猜你将这三个文件都包含在同一个文件中的某个地方,也许是main.c

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

main () { /* Do Stuff */ }

这将被扩展(当然减去 cmets)为:

// 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 &lt;GenericTypeDefs.h&gt; 确实在预处理器步骤的某个时间点多次出现。从表面上看,该文件看起来像:

#ifndef GENERIC_TYPE_DEFS_H
#define GENERIC_TYPE_DEFS_H

#define UINT8 unsigned char

#ifdef  __cplusplus
extern "C" {
#endif

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

// 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 没有包含保护,编译器将警告重复的符号定义。如果没有,请创建您自己的 MyGenericTypeDefs.h 或切换到 &lt;stdint.h&gt; 标头,它是 C 标准的一部分,并提供与您的 GenericTypeDefs.h 类似的功能。

警告:前面的解决方法不好

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

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 */ }

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

【讨论】:

  • 嗨,@kevin!也很高兴在这里见到你。感谢您的出色回答。
【解决方案2】:

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

-efile(537,GenericTypeDefs.h)

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

【讨论】:

  • 我不明白为什么这被否决了。这回答了如何抑制 lint 警告的原始问题。一般来说,最好抑制您已经审查过的单个警告,以便 linter 可以找到相同类型的未来警告。 “忽略它”的公认答案类似于忽略编译器警告。如果您只是忽略这些而不解决或抑制它们,那么有用的警告将在被忽略的警告海洋中消失。
【解决方案3】:

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

忽略它/禁用它。

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2013-07-14
    • 2019-05-26
    • 1970-01-01
    • 2014-08-23
    • 2010-11-11
    相关资源
    最近更新 更多