Lack of multiple-inclusion protection in header files

Lack of multiple-inclusion protection in header files

 

Software ResiliencyProgramming Best Practices

This code insight verifies in C++ applications that header files (.h) are protected against multiple inclusion. If a header file is not using the appropriate syntax, Highlight counts an occurrence.

Why you should care

The basic use of header files is to provide symbol declarations for functions and globals. Because multiple declarations of a given symbol in a single translation unit are a syntax error, you have to defensively structure your header files to not redefine anything in case they are included multiple times.

CAST recommendations

As Julio Merino advised in his excellent blog article on this topic, just follow this pattern and encapsulate the whole contents of the whole header file within a guard:

#if !defined(PROJECT_MODULE_H)
#define PROJECT_MODULE_H

... all header file contents go here ...

#endif  /* !defined(PROJECT_MODULE_H) */

 

References

http://julio.meroh.net/2013/11/header-files-multiple-inclusion.html

 

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

How it worksFeatures & Analytics