Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory structure for a C++ library

I am working on a C++ library. Ultimately, I would like to make it publicly available for multiple platforms (Linux and Windows at least), along with some examples and Python bindings. Work is progressing nicely, but at the moment the project is quite messy, built solely in and for Visual C++ and not multi-platform at all.

Therefore, I feel a cleanup is in order. The first thing I'd like to improve is the project's directory structure. I'd like to create a structure that is suitable for the Automake tools to allow easy compilation on multiple platforms, but I've never used these before. Since I'll still be doing (most of the) coding in Visual Studio, I'll need somewhere to keep my Visual Studio project and solution files as well.

I tried to google for terms like "C++ library directory structure", but nothing useful seems to come up. I found some very basic guidelines, but no crystal clear solutions.

While looking at some open source libraries, I came up with the following:

\mylib     \mylib <source files, read somewhere to avoid 'src' directory>         \include? or just mix .cpp and .h     \bin <compiled examples, where to put the sources?>     \python <Python bindings stuff>     \lib <compiled library>     \projects <VC++ project files, .sln goes in project root?>     \include?      README     AUTHORS     ... 

I have no/little previous experience with multi-platform development/open source projects and am quite amazed that I cannot find any good guidelines on how to structure such a project.

How should one generally structure such a library project? What ca be recommended to read? Are there some good examples?

like image 735
TC. Avatar asked Sep 09 '09 09:09

TC.


People also ask

What is directory structure?

What is directory structure? The directory structure is the organization of files into a hierarchy of folders. It should be stable and scalable; it should not fundamentally change, only be added to. Computers have used the folder metaphor for decades as a way to help users keep track of where something can be found.

What is the structure of a file directory?

Within each volume, the directory structure is hierarchical. It is an inverted tree structure with a single root. The topmost directory in the hierarchy is called the root directory. A directory is called the parent directory of the subdirectories and files in it.

What is directory in data structure?

A directory is a container that is used to contain folders and files. It organizes files and folders in a hierarchical manner. There are several logical structures of a directory, these are given below. Single-level directory – The single-level directory is the simplest directory structure.


2 Answers

One thing that's very common among Unix libraries is that they are organized such that:

./         Makefile and configure scripts. ./src      General sources ./include  Header files that expose the public interface and are to be installed ./lib      Library build directory ./bin      Tools build directory ./tools    Tools sources ./test     Test suites that should be run during a `make test` 

It somewhat reflects the traditional Unix filesystem under /usr where:

/usr/src      Sometimes contains sources for installed programs /usr/include  Default include directory /usr/lib      Standard library install path /usr/share/projectname   Contains files specific to the project. 

Of course, these may end up in /usr/local (which is the default install prefix for GNU autoconf), and they may not adhere to this structure at all.

There's no hard-and-fast rule. I personally don't organize things this way. (I avoid using a ./src/ directory at all except for the largest projects, for example. I also don't use autotools, preferring instead CMake.)

My suggestion to you is that you should choose a directory layout that makes sense for you (and your team). Do whatever is most sensible for your chosen development environment, build tools, and source control.

like image 66
greyfade Avatar answered Sep 26 '22 07:09

greyfade


There is this awesome convention that I recently came across that might be helpful: The Pitchfork Layout (also on GitHub).

To sum up, subsection 1.3 states that:

PFL prescribes several directories that should appear at the root of the project tree. Not all of the directories are required, but they have an assigned purpose, and no other directory in the filesystem may assume the role of one of these directories. That is, these directories must be the ones used if their purpose is required.

Other directories should not appear at the root.

build/: A special directory that should not be considered part of the source of the project. Used for storing ephemeral build results. must not be checked into source control. If using source control, must be ignored using source control ignore-lists.

src/: Main compilable source location. Must be present for projects with compiled components that do not use submodules. In the presence of include/, also contains private headers.

include/: Directory for public headers. May be present. May be omitted for projects that do not distinguish between private/public headers. May be omitted for projects that use submodules.

tests/: Directory for tests.

examples/: Directory for samples and examples.

external/: Directory for packages/projects to be used by the project, but not edited as part of the project.

extras/: Directory containing extra/optional submodules for the project.

data/: Directory containing non-source code aspects of the project. This might include graphics and markup files.

tools/: Directory containing development utilities, such as build and refactoring scripts

docs/: Directory for project documentation.

libs/: Directory for main project submodules.

Additionally, I think the extras/ directory is where your Python bindings should go.

like image 43
Gabriel Galli Avatar answered Sep 23 '22 07:09

Gabriel Galli