如何避免#include对外部库的依赖 [英] How to avoid #include dependency to external library

查看:189
本文介绍了如何避免#include对外部库的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用如下的头文件创建静态库:

  // Myfile.h 

#includeSomeHeaderFile.h//外部库

类MyClass
{

//我的代码

};



在我自己的项目中,我可以告诉编译器(在我的例子中,Visual Studio) SomeHeaderFile.h。但是,我不希望我的用户关心这一点 - 他们应该能够包括我的头,而不必通知他们的编译器SomeHeaderFile.h的位置。



这种类型的情况如何正常处理?

解决方案

这是一个经典的编译防火墙场景。有两个简单的解决方案:


  1. 前缀 - 从外部库中声明你需要的任何类或函数。然后,只在您的cpp文件中包含外部库的头文件(当您实际需要使用您在头文件中声明的类或函数时)。


  2. p>使用PImpl idiom(或Cheshire Cat),其中转发声明一个实现类,您只声明和定义(在cpp文件中)。您可以使用该私有类来放置所有与外部库相关的代码,以避免在您的公共类(在您的头文件中声明的)中有任何跟踪。




    1. 下面是使用第一个选项的示例:

        #ifndef MY_LIB_MY_HEADER_H 
      #define MY_LIB_MY_HEADER_H

      class some_external_class; // forward-declare external dependency。

      class my_class {
      public:
      // ...
      void someFunction(some_external_class& aRef); //使用向前声明的不完整类型声明成员。
      };

      #endif

      //在cpp文件中:

      #includemy_header.h
      #includesome_external_header.h

      void my_class :: someFunction(some_external_class& aRef){
      //这里,你可以从some_external_class中使用所有你想要的。
      };

      以下是选项2的示例:

        #ifndef MY_LIB_MY_HEADER_H 
      #define MY_LIB_MY_HEADER_H

      class my_class_impl; // forward-declare privateimplementation类。

      class my_class {
      private:
      std :: unique_ptr< my_class_impl> ; //一个消失的外观...
      public:
      // ...
      };

      #endif

      //在cpp文件中:

      #includemy_header.h
      #includesome_external_header.h

      class my_class_impl {
      private:
      some_external_class obj;
      // ...
      public:
      // some functions ...
      };

      my_class :: my_class():pimpl(new my_class_impl()){};


      If I'm creating a static library with a header file such as this:

      // Myfile.h
      
      #include "SomeHeaderFile.h" // External library
      
      Class MyClass
      {
      
      // My code
      
      };
      

      Within my own project I can tell the compiler (in my case, Visual Studio) where to look for SomeHeaderFile.h. However, I don't want my users to be concerned with this - they should be able to include my header without having to inform their compiler about the location of SomeHeaderFile.h.

      How is this type of situation normally handled?

      解决方案

      This is a classic "compilation firewall" scenario. There are two simple solutions to do:

      1. Forward-declare any classes or functions that you need from the external library. And then include the external library's header file only within your cpp file (when you actually need to use the classes or functions that you forward-declared in your header).

      2. Use the PImpl idiom (or Cheshire Cat) where you forward-declare an "implementation" class that you declare and define only privately (in the cpp file). You use that private class to put all the external-library-dependent code to avoid having any traces of it in your public class (the one declared in your header file).

      Here is an example using the first option:

      #ifndef MY_LIB_MY_HEADER_H
      #define MY_LIB_MY_HEADER_H
      
      class some_external_class;  // forward-declare external dependency.
      
      class my_class {
        public:
          // ...
          void someFunction(some_external_class& aRef);  // declare members using the forward-declared incomplete type.
      };
      
      #endif
      
      // in the cpp file:
      
      #include "my_header.h"
      #include "some_external_header.h"
      
      void my_class::someFunction(some_external_class& aRef) {
        // here, you can use all that you want from some_external_class.
      };
      

      Here is an example of option 2:

      #ifndef MY_LIB_MY_HEADER_H
      #define MY_LIB_MY_HEADER_H
      
      class my_class_impl;  // forward-declare private "implementation" class.
      
      class my_class {
        private:
          std::unique_ptr<my_class_impl> pimpl; // a vanishing facade...
        public:
          // ...
      };
      
      #endif
      
      // in the cpp file:
      
      #include "my_header.h"
      #include "some_external_header.h"
      
      class my_class_impl {
        private:
          some_external_class obj;
          // ...
        public:
          // some functions ... 
      };
      
      my_class::my_class() : pimpl(new my_class_impl()) { };
      

      这篇关于如何避免#include对外部库的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆