In C/C++ I have written a header file which is available to all, however I want to restrict the files that include it. Is there any way I can generate compiler error if my header files is included by an "unauthorized" c/cxx file?
Note: We can't include the same header file twice in any program. Create your own Header File: Instead of writing a large and complex code, we can create your own header files and include them in our program to use it whenever we want.
It attempts to find a function definition (implementation), which exactly matches the header you declared earlier. What happens if you #include header file is that compiler (specifically, the preprocessor) copies the whole contents of header file into place, where you put your #include .
In C language, header files contain the set of predefined standard library functions. The “#include” preprocessing directive is used to include the header files with “. h” extension in the program.
As others have noted, it helps ensure that the declarations in the header file are consistent with the definitions in the C file. This is of particular importance when some or all of the routines are to be called from another module. In other words, this can help cut down on errors.
Of course there is no proper security feature for this (that'd be plain silly), but what you could do is to produce an error in your header file if a certain macro is not present when including the file, so that your header.h
starts with
#ifndef AUTHORIZED_TO_INCLUDE_THE_HEADER
#error "You're not authorized to include this file"
#endif
then in the files that include this, do
#define AUTHORIZED_TO_INCLUDE_THE_HEADER
#include "header.h"
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