So there is third-party library that has a header file you need to include in order to use it. Since the implementation of the library is not object oriented I wrote a Class to encapsulate all usage of the library, so in case it needs to be replaced I can just modify the implementation of that class.
Since other developers will be working in the same code base I want a way to give them an error if they include the library. This is to avoid having references all over the place to the library.
For example if they do something like this:
#include "cool_library.h"
they get an error saying:
do not include directly cool_library.h, instead use the cool_library_wrapper class
is this possible? I'm using GNU GCC
Since you're using gcc, you can use the #include_next feature of the preprocessor: Create a header with the same name as the 3rd party's in a directory that will have higher precedence when looking for header files. In your version of the header use something like
#if WRAPPER_HEADER_HAS_BEEN_INCLUDED
# include_next <cool_library.h>
#else
# error ...
#endif
You could use a #error preprocessor directive within a #ifndef block.
For example in the orginal .h file have this:
#ifndef COOL_LIBRARY_WRAPPER_CLASS_INCLUDED
#error "do not include this file directly
#endif
And in the wrapper class's header file do this:
#define COOL_LIBRARY_WRAPPER_CLASS_INCLUDED
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