PC Lint 警告 537:重复包含文件
如何处理 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
上重复#include
,cryptography.c
和crc8.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用选项禁用该标头的警告:
并将其添加到您的 Lint 配置中(如果有)。
You can disable the warning just for that header with the option:
and add that to your Lint config if you have one.
您链接到的文章解释说,这更像是 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.
忽略它
如果你有包含防护,这是一个虚假的警告,可以(应该)被忽略。我们使用包含防护,因为允许多次包含文件是一种很好的做法,可以实现更灵活的代码,防止人为错误,并避免在
#include
语句中具有顺序重要性。如果您想知道为什么会出现这种情况,请继续阅读。多个包含不是问题,因为你有包含防护。
由于这些 .h 文件都非常相关,我猜测您将所有三个文件包含在同一个文件中的某个位置,也许是
main.c
:这将被扩展(当然,减去注释)到:
所以,是的,
#include
在预处理器步骤中的某个时刻确实出现了多次。表面上,该文件看起来像:因此,经过更多预处理后,我们之前扩展的位(减去注释)变为:
进一步的预处理(未显示)在整个代码中复制
#define
。如果您留意其他预处理器和 linter 警告,则保证您拥有包含防护
。如果任何
.h
文件缺少标准包含防护,linter 将向您发出警告(错误 451 和 967)。如果多重包含的GenericTypeDefs.h
没有包含保护,编译器将警告重复的符号定义。如果没有,请创建您自己的MyGenericTypeDefs.h
或切换到
头文件,它是 C 标准的一部分,提供与您的类似的功能GenericTypeDefs.h
。警告:前面的解决方法不好
如果您确实坚持修复警告而不是忽略它,则必须从每个
.h
中删除#include
> 文件并在包含一个或多个文件之前输入一次,如下所示:checksum.c
cryptography.c
crc.c
main.c
不推荐这样做,因为它会导致您做更多的工作,并且有更多的机会犯错误(无意冒犯,但您只是人类,而预处理器不是)。相反,只需要求预处理器完成其工作并按照设计方式使用包含防护即可。
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
:This will be expanded (minus the comments, of course) to:
So yes,
#include <GenericTypeDefs.h>
does appear multiple times at some point in the preprocessor step. Ostensibly, the file looks something like:so, after more preprocessing, our previously expanded bit (minus the comments) becomes:
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-includedGenericTypeDefs.h
doesn't have include guards. If it does not, either create your ownMyGenericTypeDefs.h
or switch to the<stdint.h>
header, which is part of the C standard and provides similar functionality as yourGenericTypeDefs.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
cryptography.c
crc.c
main.c
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.