开发者

how to prohibit other developers to #include a third party header in C++

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


This would only be possible if you're okay with modifying the cool_library.h file. You could do something like:

cool_library.h

#ifndef INCLUDED_FROM_COOL_LIBRARY_WRAPPER
#error do not include directly cool_library.h, instead use the cool_library_wrapper class
#endif

.. remainder of original cool_library.h

cool_library_wrapper.h

#define INCLUDED_FROM_COOL_LIBRARY_WRAPPER
#include "cool_library.h"

... your wrapper

#undef INCLUDED_FROM_COOL_LIBRARY_WRAPPER

Of course, you still can't prevent your coworkers from defining INCLUDED_FROM_COOL_LIBRARY_WRAPPER themselves and including the original header file. This is a social problem that does not have a technical solution.


If you usually include some project-wide headers you could check for the presence of the include guard from the third-party header, e.g.

// third_party.h
#ifndef THIRD_PARTY_H
#define THIRD_PARTY_H
...

and

// your_project_wide.h
...
#ifdef THIRD_PARTY_H
#warning "Please include "cool_library.h"
#endif
...

Caveats here: #warning is a gcc extension, and all this relies on the external dependencies being included before your project headers (which you might not want to do).


Don't put <cool_library.h> in the normal build include path. You can either use special CFLAGS for your wrapper to give it access or access it with a more explicit path like <vendor/xyz/cool_library.h> from some higher-level include path.

Another path-based approach would be to put a local <cool_library.h> earlier in the include path and use the #ifdef/#error approach above. If the magic define is present then the stub header can use the more explicit path to pick up the real header. (Some compilers have a hack to continue the search path if you include something named exactly the same thing as the header that's being read)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜