Sonntag, 30. November 2014

Quick C Tricks - Structured Include Path

In a larger C project you might want to have your API (=the collection of your header files) more structured instead of throwing all the header files into one flat directory.

The cpputest project is using a nice and simple approach. Their software (a C and C++ unit test library we are using at work) consists out of core functions and extended functionality.

Here is a sketch of the include path layout:
/.../cpputest/include/
                      CppUTest/
                              MemoryLeakDetector.h
                              TestHarness.h
                                ...
                      CppUTestExt/
                              GMock.h
                              MockFailure.h
                              ...  
The compiler gets the main include directory -I/.../cpputest/include/. In your code (for example to start writing a unit test) you include the remaining path:
#include <CppUTest/TestHarness.h>
... 
Looking at this include line you instandly know that TestHarness.h belongs to CppUTest and (if you are familiar with the library) it is part of the core functionality (=not inside CppUTestExt).

This structuring approach gives you simple namespacing of your headers - which also your Java co-works will appriciate ;-)

Keine Kommentare:

Kommentar veröffentlichen