Search code examples
c++makefiledirectory-structure

How to structure a "library" of C++ source?


I'm developing a collection of C++ classes and am struggling with how to share the code in a way that maintains organization without compromising ease of compilation for a user of the collection.

Options that I have seen include:

  • Distribute compiled library file
  • Put the source in the header file (with implicit inline as discussed in this answer)
  • Use symbolic links to allow the compiler to find the files.

I'm currently using the third option where, for each class the I want to include I symbolic link each classess headers and source files (e.g. ln -s <path_to_class folder>/myclass.cpp) This works well except that I can't move the project folder location (it breaks all the symlinks) and I have to have all those symlinked files hanging around.

I like the second option (it has the appearance of Java), but I'm worried about code size bloat if everything is declared inline.

A user of the collection will create a project folder somewhere, and somehow include the collection into their compilation process.

I'd like a few things to be possible:

  1. Easy compilation (something like gcc *.cpp from the project folder)
  2. Easy distribution of library in uncompiled form.
  3. Library organization by module.
  4. Compiled code size is not bloated.

I'm not worried about documentation (Doxygen takes care of that) or compile time: the overall modules are small and even the largest projects on the slowest machines won't take more than a few seconds to compile.

I'm using the GCC compiler, if it makes any difference.


Solution

  • I ended up using inline headers for all of the code. You can see the library here:

    https://github.com/libpropeller/libpropeller/tree/master/libpropeller

    The library is structured as:

    • library folder
      • class A
        • classA.h
        • classA.test.h
      • class B
        • classB.h
        • classB.test.h
      • class C
      • ...

    With this structure I can distribute the library as source, and all the user has to do is include -I/path/to/library in their makefile, and #include "library/classA/classA.h" in their source files.

    And, as it turns out, having inline headers actually reduces the code size. I've done a full analysis of this, and it turns out that inline code in the headers allows the compiler to make the final binary roughly 5% smaller.