And as far as I understand it, I need to export the public API of a shared library through 1 single header file. |
No, you don't have to. Many libraries, especially more complex ones, come with a large number of header files! Note: Wherever the user
#include's your header file,
everything in that header file gets "imported" into the user's namespace. Therefore you want to reduce the "public" header file(s) to the bare minimum! For example, don't define anything there that could be internal to your library. Also, at some point, you may want to split the header file into several header files, so that the user can decide to
#include just what they really need...
For example, if you have several classes that are closely related and that are almost always used together, then it could make sense to merge the
declarations of those classes in a single header file. In the general case, however, I would tend to create a separate header file for each class, in order to have a clean separation and to
not clutter the user's namespace with (probably) unneeded declarations.
The
C standard library actually is a good example of this, as its public API is split into various header files for different purposes:
https://en.wikipedia.org/wiki/C_standard_library#Application_programming_interface_(API)
Should I transfer all the public information to the public API? |
Yeah, you should think of the "public" header file(s), i.e. the
.h
file(s) that are usually provided in an
include
directory, alongside with the actual library, as the
public interface (API) of your library. It is supposed to contain
everything that the user needs to call into your library. It also means that anything which is
not considered to be part of the public API of your library
should not be in the "public" header files.
Keep in mind: Everything you put into the "public" header files becomes a contract between the application and your library. You won't be able to change those things afterwards (without breaking existing applications), so be very careful what you decide to put in the "public" header files! Meanwhile, anything that remains "internal" to your library can usually be changed quite easily...
How exactly you organize your "public" header files (e.g. whether you use one or several header files), that is totally up to you 😉