Isolating Dependencies in C++ Code

When working in C++, one of the main problems is the complexity of interfaces. The constant use of header files in the language make it difficult to avoid the inflation of translation unit sizes – a single file can include dozens of headers, which can do the same recursively. As a result, even small source files can take a long time to compile.

The issue becomes even more pronounced when you consider templates. The fact that they need to be textually included when invoked in a source file has consequences for the size of the translation units. In the end they just add even more to the amount of work the compiler needs to complete for each translation unit.

There are some techniques, however, that can be valuable in overcoming these issues. One particular technique that I use frequently is avoiding direct dependencies on external libraries in my source files. This technique may be hard to use in some situations, but for smaller libraries it works really well.

Minimizing Dependencies

The idea is that you don’t need to include directly the header file of a third-party library in you code. Instead, you can create a single source file that includes the library’s header file – and then create you own classes or functions that export the functionality to the rest of the application.

For example, when using a compression library, instead of adding the header file from the third party vendor, I just create a new pair of source file and a header file:

  • compress.cpp and
  • compress.h

The first file includes the external header. The last one provides a simple interface that I will use in the rest of the source code. This has a number of advantages:

  • The dependency is centralized. If, in the future I need to use another library, I will just need o change one source file. Everything else will compile correctly because I control the code.
  • The interface is easy to use. Since I am the creator of the simple interface in the header file,  I will add only the functionality that matters, and nothing else.
  • The interface is small. As you are including the external header file only once, this will reduce your total compilation time. Your local interface will have only the small number of elements that you really need.

Creating simple interfaces can make your life much easier, and reduce maintenance risks. It is a win-win scenario for developers, since this can reduce compilation times, and minimize future integration costs.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

2 Responses to “Isolating Dependencies in C++ Code”

  1. Hello,

    Do you mean to put all the headers files into
    compress.cpp ?


    By Kevn on Sep 10, 2015

  2. Yes, the idea is that for each library I would only include its header file into my single interface class. Later, I would use only my interface class in the rest of the project.

    By coliveira on Sep 10, 2015

Post a Comment