#include "file"
means take the header file and put all of its content instead of the #include
line.
We usually used headers for type definitions and for adding a forward declarations to a source files. defining same type twice in a file (a circular include will always cause it) gives compilation error, therefore we use #ifndef
or #pragma once
. (or both)
But we also can to put a repeating code and macros and include it several times, even in the same file. in such as case, we won't use #ifndef
nor #pragma once
. If you do so you must be extra careful, and do it only if you know what you are doing.
For example: If in some OS calling a specific system function (or even a c macro like: offsetof
) cause a bunch of warnings, and it is bothering you, and you sure your code is good, but you don't want to disable all the warnings you've got on all the project or the file, you just want to disable it when you call the specific function.
//suppress the warnings:
#if defined(__GNUC__)
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic ignored "-Wsequence-point"
#endif
#endif // __GNUC__
//here you call the function...
func(x,y,z);
//unsupress: bring back the warnings to normal state
#if defined(__GNUC__)
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic pop
#endif
#endif // __GNUC__
This will make your code to look very dirty, especially if you call the function several times.
One possible solution, (I'm not suggesting it is the best one...) is to make 2 headers, in one to suppress the warnings and in the other to cancel the suppression.
In that case your code may look like this:
#include "suppress.h"
func(x,y,z);
#include "unsuppress.h"
//.... more code come here
//now when call it again:
#include "suppress.h"
func(x,y,z);
#include "unsuppress.h"