如何避免#include依赖于外部库

时间:2022-10-25 15:08:09

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.

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

How is this type of situation normally handled?

这种情况通常如何处理?

2 个解决方案

#1


13  

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).

    从外部库转发声明您需要的任何类或函数。然后在您的cpp文件中包含外部库的头文件(当您实际需要使用在头中声明的类或函数时)。

  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).

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

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:

以下是选项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()) { };

#2


4  

Say the external header file contains the following:

假设外部头文件包含以下内容:

external.h

external.h

class foo
{
public:
   foo();
};

And in your library you use foo:

在您的图书馆中,您使用foo:

myheader.h:

myheader.h:

#include "external.h"

class bar
{
...
private:
   foo* _x;
};

To get your code to compile, all you have to do is to forward declare the foo class (after that you can remove the include):

要使代码编译,您所要做的就是转发声明foo类(之后可以删除include):

class foo;

class bar
{
...
private:
   foo* _x;
};

You would then have to include external.h in your source file.

然后,您必须在源文件中包含external.h。

#1


13  

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).

    从外部库转发声明您需要的任何类或函数。然后在您的cpp文件中包含外部库的头文件(当您实际需要使用在头中声明的类或函数时)。

  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).

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

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:

以下是选项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()) { };

#2


4  

Say the external header file contains the following:

假设外部头文件包含以下内容:

external.h

external.h

class foo
{
public:
   foo();
};

And in your library you use foo:

在您的图书馆中,您使用foo:

myheader.h:

myheader.h:

#include "external.h"

class bar
{
...
private:
   foo* _x;
};

To get your code to compile, all you have to do is to forward declare the foo class (after that you can remove the include):

要使代码编译,您所要做的就是转发声明foo类(之后可以删除include):

class foo;

class bar
{
...
private:
   foo* _x;
};

You would then have to include external.h in your source file.

然后,您必须在源文件中包含external.h。