Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure private and public API in C++ (header files)

I am currently developing an API and I now find useful to have an explicit split in my directory tree in order to separate public header files and private header and source files. This way, it is going to be easier to build both the public and the development (private) package for my SDK. I have now in mind two different options:

1)
.../LibName/Class.h
.../LibName/private/ClassImpl.h
.../LibName/private/LibName/ClassImpl.cpp

2)
.../public/LibName/Class.h
.../private/LibName/ClassImpl.h
.../private/LibName/ClassImpl.cpp

It would be nice to have files organized in the way 'Libname/FileName' at least for the public files, because this is the way include directives are in my code:

#include "LibName/FileName.h"

What do you think is the best option to organize the headers for an API? One of these two or maybe some other?

Thank you

like image 555
chorch Avatar asked Dec 23 '13 17:12

chorch


1 Answers

I finally chosed the following structure:

.../include/LibName1/Class.h
.../include/LibName2/OtherClas.h

.../src/LibName1/ClassImpl.h
.../src/LibName1/ClassImpl.cpp
.../src/LibName2/OtherClassImpl.h
.../src/LibName2/OtherClassImpl.h

This way I can both separate public headers (include directory) and private source (src directory) and also reference the header files in the include directives without writing explicitly if they are public or private:

#include "LibName1/Class.h"

This structure (or a similar one) is also used in other libraries like Qt framework.

like image 179
chorch Avatar answered Sep 23 '22 14:09

chorch