# A question on style regarding writing a small library (C++)

This topic is 1753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

[Removed]

Edited by Shaquil

##### Share on other sites

Hi

If I am to use your library I'd much rather the classes to be in separate .h/.cpp files.. it makes it much more readable...

After that if you want u can always add one single .h file that includes all classes at once but still keeping the actual class code in separate files.

##### Share on other sites

If I am to use your library I'd much rather the classes to be in separate .h/.cpp files.. it makes it much more readable...

After that if you want u can always add one single .h file that includes all classes at once but still keeping the actual class code in separate files.

QFE.

One header + cpp file per class (though small utility classes can be grouped together if related), and one <library-name>.h header file that includes everything I need to use the library.

##### Share on other sites

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

My "preference" is definitely not to have all encompassing headers.  I work on a conceptual basis for my code.  An example from my math library is that yes it has a "Math.hpp" file you can include and it takes care of a lot of things.  It defines any primitive types used, includes other concepts if required etc.  So, for instance, using my math library I can do the following:

some.hpp

#include <Math/Math.hpp>

class Blah
{
public:
Blah( Math::Vector3fv& position );
};


That compiles no problem.  What won't compile is:

#include <Math/Math.hpp>

class Blah
{
public:
Blah( Math::Vector3fv& position );

private:
Math::Vector3fv   mPosition;
};


The general header only includes the forward reference, not the class.  As such you have to explicitly add an include, but hopefully I make it very easy:

#include <Math/Math.hpp>
#include <Math/Vector3fv.hpp>

class Blah
{
public:
Blah( Math::Vector3fv& position );

private:
Math::Vector3fv   mPosition;
};


That fixes it for this file.  I'm very explicit in making this easy to figure out since namespace == the directory name, class == file name and as such just replace :: with slash and append hpp and you are done.

In the classes themselves, if Vector3fv absolutely must include Matrix or something else to function, it includes it.  I don't leave it up to the user of the code to go figure out implied dependencies...

This is of course one way to do things, I like it and have seen it in other code bases.  Is it for you, that's your choice.

<Sorry if you saw this cut in half, GD didn't seem to save the whole thing the first time.>

Edited by Hiwas

##### Share on other sites

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

To be absolutely and 100% clear, because I've discovered lately that a lot of people missed this class:

The single, global header is only to be used by clients of your library.

If you globally include <library-name>.h within your library, I will hunt you down, and lecture you on compile times

##### Share on other sites

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

To be absolutely and 100% clear, because I've discovered lately that a lot of people missed this class:

The single, global header is only to be used by clients of your library.

If you globally include <library-name>.h within your library, I will hunt you down, and lecture you on compile times

Err, oops.  <Extracts foot from mouth!>

Though to be honest, I still tend to leave the separated headers in the libraries as personal preference.  I still think it a bit too likely that someone will link another big file to that one and end up with the chain of massive includes.  I guess if you are willing to stand on folks heads and ask: "You will NEVER ever include big includes from other big includes again, will you????", then it works out.

Edited by Hiwas

##### Share on other sites

When I break things into .h/.cpp files, I like to break them up based on their "idea".  if it were java, I would make 1 .java file per class, no matter how big or small.  But with C++ if i have a class that relies on some small struct, I will normally include the struct in the same .h file and inline any methods associated with that struct.  This is simply because in C++, unlike java, an additional include file really DOES matter a lot for compile time, and also, we get the added benefit of the methods being inlined.  However, as soon as I see myself forming a new idea with either new classes, or structs that go beyond that "POD" state, I do branch out into a new .h/.cpp combo.

As for the library itself...it depends what you're making.  If you're making a library that is meant to be included as a binary, I think having a "Library.h" file that includes your library itself or many basic types, that makes sense.   The only downside is that if the user upgrades the library, their project will need to recompile all sources that include anything from your library, but let's be frank: if they upgrade a library and don't expect that, what planet are they living on?

Edited by metsfan