Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header files dependencies between C++ modules

In my place we have a big C++ code base and I think there's a problem how header files are used.

There're many Visual Studio project, but the problem is in concept and is not related to VS. Each project is a module, performing particular functionality. Each project/module is compiled to library or binary. Each project has a directory containing all source files - *.cpp and *.h. Some header files are API of the module (I mean the to the subset of header files declaring API of the created library), some are internal to it.

Now to the problem - when module A needs to work with module B, than A adds B's source directory to include search path. Therefore all B's module internal headers are seen by A at compilation time.

As a side effect, developer is not forced to concentrate what is the exact API of each module, which I consider a bad habit anyway.

I consider an options how it should be on the first place. I thought about creating in each project a dedicated directory containing interface header files only. A client module wishing to use the module is permitted to include the interface directory only.

Is this approach ok? How the problem is solved in your place?

UPD On my previous place, the development was done on Linux with g++/gmake and we indeed used to install API header files to a common directory is some of answers propose. Now we have Windows (Visual Studio)/Linux (g++) project using cmake to generate project files. How I force the prebuild install of API header files in Visual Studio?

Thanks Dmitry

like image 444
dimba Avatar asked Jul 21 '09 16:07

dimba


2 Answers

It sounds like your on the right track. Many third party libraries do this same sort of thing. For example:

3rdParty/myLib/src/                  -contains the headers and source files needed to compile the library
3rdParty/myLib/include/myLib/  - contains the headers needed for external applications to include

Some people/projects just put the headers to be included by external apps in /3rdParty/myLib/include, but adding the additional myLib directory can help to avoid name collisions.

Assuming your using the structure: 3rdParty/myLib/include/myLib/


In Makefile of external app:
---------------
INCLUDE =-I$(3RD_PARTY_PATH)/myLib/include
INCLUDE+=-I$(3RD_PARTY_PATH)/myLib2/include
...
...

In Source/Headers of the external app
#include "myLib/base.h"
#include "myLib/object.h"

#include "myLib2/base.h"
like image 111
RC. Avatar answered Oct 21 '22 05:10

RC.


Wouldn't it be more intuitive to put the interface headers in the root of the project, and make a subfolder (call it 'internal' or 'helper' or something like that) for the non-API headers?

like image 24
jalf Avatar answered Oct 21 '22 07:10

jalf