Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kinds of header files should not be protected against multiple inclusion?

I read the dcmtk source code, and found a comment in ofstdinc.h:

// this file is not and should not be protected against multiple inclusion

And what kinds of header files SHOULD NOT be protected against multiple inclusion?

like image 961
sfz Avatar asked May 09 '15 12:05

sfz


People also ask

How do you prevent multiple inclusions of header files?

To avoid multiple inclusions of the same header file we use the #ifndef, #define and #endif preprocessor directives. Just write the entire program in only file and include headers once only.

What are the types of header files?

There are of 2 types of header file: Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them. User-defined header files: These files are defined by the user and can be imported using “#include”.

Which header file should be included to work with files?

You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio. h header file, which comes along with your compiler.

Is it acceptable for one header file to #include another?

No problem. Making codebases a better place one line of code at a time :) Also, are you saying I can add both my #include <iostream> and #include "WorldState. h" inside global.


Video Answer


2 Answers

Preprocessor metaprogramming. That is, using the included file as a sort of compile-time function that performs some task. The arguments to the function are macros. For example, the file you linked has a section that looks like this:

// define INCLUDE_STACK to include "ofstack.h"
#ifdef INCLUDE_STACK
#include "dcmtk/ofstd/ofstack.h"
#endif

So if I wanted to include "ofstack.h", I would do so like this:

#define INCLUDE_STACK
#include "ofstdinc.h"
#undef INCLUDE_STACK

Now, imagine later down the line, someone wants to use this particular section of the header:

// define INCLUDE_STRING to include "ofstring.h"
#ifdef INCLUDE_STRING
#include "dcmtk/ofstd/ofstring.h"
#endif

So they do the following:

#define INCLUDE_STRING
#include "ofstdinc.h"
#undef INCLUDE_STRING

If "ofstdinc.h" had include guards, it wouldn't be included.

like image 101
Benjamin Lindley Avatar answered Oct 17 '22 18:10

Benjamin Lindley


One example are header files which expect you to define a macro. Consider a header m.h with

M( foo, "foo" )
M( bar, "bar" )
M( baz, "baz" )

This can be used in some other header like this:

#ifndef OTHER_H
#define OTHER_H

namespace other
{
    enum class my_enum
    {
#define M( k, v ) k,
#include "m.h"
#undef M
    };

    void register_my_enum();
}

#endif

and in some other file (possibly implementation):

#include "other.h"

namespace other
{
    template< typename E >
    void register_enum_string( E e, const char* s ) { ... }

    void register_my_enum()
    {
#define M( k, v ) register_enum_string( k, v );
#include "m.h"
#undef M
    }
}
like image 43
Daniel Frey Avatar answered Oct 17 '22 17:10

Daniel Frey