Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PC Lint Warning 537: Repeated include file

Tags:

c

lint

pc-lint

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

like image 820
Daniel Grillo Avatar asked Sep 26 '11 12:09

Daniel Grillo


2 Answers

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.

like image 98
Kevin Vermeer Avatar answered Oct 22 '22 18:10

Kevin Vermeer


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.

like image 38
undur_gongor Avatar answered Oct 22 '22 19:10

undur_gongor