Source file organisation

c++, include, path

Solution

You shouldn't, in general, add source files from libraries directly to other projects. Compile them separatly as a library and use those.

For organising the library's directory structure itself, right now I settled on something like the following structure

- library1/widget.h

- library1/private/onlyinlib.h

- library1/private/widget.cpp

(and if applicable)

- library1/private/resources/widget.jpg

- library1/private/project/widget.xcode

I put all headers directly in the library path, and have a subfolder `private` which will contain everything that's only used by the library, but should never be shared / exposed.

The greatest advantage is that every project I start only needs a include path pointing at the directory containing my libraries, then every (public) include is done like

#include "library1/widget.h"

private includes are simply

#include "onlyinlib.h"

This has a number of advantages:

- If new libraries are introduced, there's no messing with project /compiler settings to get the headers 'visible'.

- Moving to other compilers / platforms is also very little hassle.

- The headers are automatically 'namespaced', i.e. by including part of the path too, it's next to impossible to get a nameclash with the includes

- It's immediatly obvious where a header comes from, and if a header is part of the public interface or not

Problem

I am having a bit of trouble organising my source files. I have my own small, but growing collection of code that I would like to use in various projects. The file and folder layout is something like this: library\sub1\source.h library\sub1\source.cpp library\sub2\source.h library\sub2\source.cpp One of my problems is that I want to include this code, as needed, in my other projects. To date I have used absolute paths to point to the libary code, but there must be a better way. Futhermore, I need to add every library file I use to a project's files Visual Studio in order for it to compile correctly. So my question in short is how do I fix this? What is the proper/best way to handle the above situation.

Original source