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

发布于 2024-12-06 16:17:12 字数 1324 浏览 1 评论 0原文

如何处理 PC Lint 的此警告?

我在几个文件中都有#include。 PC Lint 向我显示消息警告 537:重复包含文件 '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

< code>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.c上重复#includecryptography.ccrc8.c

How to deal with this warning from PC Lint?

I have in several files the #include <GenericTypeDefs.h>. PC Lint shows me the message Warning 537: Repeated include file 'filepath\filename.h' If I delete this declaration I cannot compile.

I would like to suppress this warning if possible.

You can see the same reported here.

This is my code, for which my compiler emits warnings:

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

Obviously, I didn't repeat #include <GenericTypeDefs.h> on checksum.c, cryptography.c and crc8.c

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请帮我爱他 2024-12-13 16:17:13

您可以使用选项禁用该标头的警告:

-efile(537,GenericTypeDefs.h)

并将其添加到您的 Lint 配置中(如果有)。

You can disable the warning just for that header with the option:

-efile(537,GenericTypeDefs.h)

and add that to your Lint config if you have one.

倾城°AllureLove 2024-12-13 16:17:13

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

只需忽略它/禁用它即可。

The article you linked to explains that it's more of a PCLint oddity, that should be a Note rather than a Warning.

Just ignore it / disable it.

蝶舞 2024-12-13 16:17:12

忽略它

如果你有包含防护,这是一个虚假的警告,可以(应该)被忽略。我们使用包含防护,因为允许多次包含文件是一种很好的做法,可以实现更灵活的代码,防止人为错误,并避免在 #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 在预处理器步骤中的某个时刻确实出现了多次。表面上,该文件看起来像:

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

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

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

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

不推荐这样做,因为它会导致您做更多的工作,并且有更多的机会犯错误(无意冒犯,但您只是人类,而预处理器不是)。相反,只需要求预处理器完成其工作并按照设计方式使用包含防护即可。

Just ignore it

If you have include guards, this is a spurious warning and can (should) be ignored. We use include guards because allowing a file to be included multiple times is good practice, enabling more flexible code, prevents human errors, and avoids having order significance in your #include statements. If you want to know why this is the case, read on.

Multiple includes aren't a problem because you have include guards.

Since these .h files are all pretty related, I'm guessing that you include all three in the same file somewhere, perhaps main.c:

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

main () { /* Do Stuff */ }

This will be expanded (minus the comments, of course) to:

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

So yes, #include <GenericTypeDefs.h> does appear multiple times at some point in the preprocessor step. Ostensibly, the file looks something like:

#ifndef GENERIC_TYPE_DEFS_H
#define GENERIC_TYPE_DEFS_H

#define UINT8 unsigned char

#ifdef  __cplusplus
extern "C" {
#endif

so, after more preprocessing, our previously expanded bit (minus the comments) becomes:

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

Further preprocessing (not shown) copies the #define throughout the code.

You are guaranteed to have include guards if you heed other preprocessor and linter warnings

The linter will warn you (errors 451 and 967) if any .h files lack standard include guards. The compiler will warn about duplicate symbol definitions if the multiply-included GenericTypeDefs.h doesn't have include guards. If it does not, either create your own MyGenericTypeDefs.h or switch to the <stdint.h> header, which is part of the C standard and provides similar functionality as your GenericTypeDefs.h.

Warning: Bad workaround ahead

If you really insist on fixing the warning instead of ignoring it, you will have to remove #include <GenericTypeDefs.h> from each of the .h files and enter it once before one or more of these files are included, like this:

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

This is Not Recommended, as it results in more work for you and more chances for you to make mistakes (no offense, but you're only human and the preprocessor is not). Instead, just ask the preprocessor to do its job and use the include guards the way they were designed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文