Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid #include dependency to external library

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?

like image 573
JBentley Avatar asked Dec 20 '12 19:12

JBentley


2 Answers

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()) { };
like image 66
Mikael Persson Avatar answered Nov 11 '22 05:11

Mikael Persson


Say the external header file contains the following:

external.h

class foo
{
public:
   foo();
};

And in your library you use foo:

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

class foo;

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

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

like image 37
Mustafa Ozturk Avatar answered Nov 11 '22 04:11

Mustafa Ozturk