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?
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.
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”.
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.
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With