C++ Project Structure question

I'm curious about how C++ developers like to lay out their projects. I'm speaking specifically about the directory layout on the file system. The best guidance I have found on this is here:

http://stackoverflow.com/questions/1398445/directory-structure-for-a-c-library

The number one answer recommends having directories for src, include, lib, bin, tools, and test. That sort of falls short of what I had in mind.

I'm trying to imagine my project when it's large, having hundreds (possibly thousands) of files in the src directory. Since I'm coming from java and maven, I'm used to having a well defined directory structure. For example, I would have something like this: src/main/java/rootPackage/subPackage/MyClass.java

I consider java packages to be similar to C++ namespaces, except for the restrictions that packages place on the directory layout. The one problem I foresee with trying that in C++ is that if you move the files into different directories, you will also be forced to update your include statements. Maybe there is a shortcut to do that, I don't know.

So I guess my first question is:

Do C++ projects tend to have a sub-directory per namespace? Or do you instead have sub-directories based on what you think helps? Or do you generally avoid having sub-directories in your src directory?

Also, what files do you tend to keep under src? Obviously you keep the .cpp files there, but do you also keep the .o files there? I know there is a separate include directory for headers, do you tend to have the include directory mirror the layout of your source directory?

If you are in the habit of just keeping all source files immediately inside src, what do you do when you have hundreds of files?
Both my hobbyist projects and SFML handle namespace and directory nesting just fine.
msknapp84 wrote:
The one problem I foresee with trying that in C++ is that if you move the files into different directories, you will also be forced to update your include statements. Maybe there is a shortcut to do that, I don't know.
Java is the same way.
msknapp84 wrote:
Do C++ projects tend to have a sub-directory per namespace? Or do you instead have sub-directories based on what you think helps? Or do you generally avoid having sub-directories in your src directory?
It's developer preference. I like to match my directories to my namespaces. Some people don't even use subdirectories at all but they use namespaces, which kind of defeats the purpose in my opinion.
msknapp84 wrote:
Also, what files do you tend to keep under src? Obviously you keep the .cpp files there, but do you also keep the .o files there? I know there is a separate include directory for headers, do you tend to have the include directory mirror the layout of your source directory?
Build artifacts are never kept - they are specific to your system alone. Some projects do not even support in-source builds (e.g. clang/LLVM). And yes I mirror the directory layout - it seems redundant but the goal is that the end user should just be able to copy the include folder and not have to worry about removing the cpp files.
msknapp84 wrote:
If you are in the habit of just keeping all source files immediately inside src, what do you do when you have hundreds of files?
I don't know, I see it in old C libraries and the file names are all gibberish.

By the way, I too have used Maven for my Java projects, so I know where you're coming from. In C++ the most commonly used build tool is CMake, and unfortunately you have to jump through some hoops to force it to understand what the magical word "subdirectory" means.
Last edited on
Java is the same way.

most java IDEs have the ability to automatically fix your package names and imports when you move a file, do the C++ IDEs support a similar capability?

Build artifacts are never kept

right, I mean I can have git ignore my .o files, I guess I'm just figuring out how to write my make file, but I suppose it doesn't matter. I could have them temporarily put in the same directory, and cleaned later.

I like to match my directories to my namespaces

is it tough to write a make file for this? Any guidelines on how to write the make file when your src directory has sub directories? I was thinking of either using pattern rules:

http://www.gnu.org/software/make/manual/make.html#Pattern-Rules

or their auto build stuff:

http://www.gnu.org/software/make/manual/make.html#Automatic-Prerequisites

but both seem kind of complicated.
Do C++ projects tend to have a sub-directory per namespace? Or do you instead have sub-directories based on what you think helps? Or do you generally avoid having sub-directories in your src directory?

As an anecdotal example, the project I work on weighs in about 175 million lines of code. The source tree is split into subdirectories for "package groups" (each builds into a different library), each package group subdirectory has "package" subdirectories (each introduces its own namespace), each package subdirectory contains source and header files directly. Seems to work so far.
I have never written a makefile in my life. I use CMake.

As for moving and renaming files support in IDEs, I haven't had the luxury to find out.
Last edited on
Topic archived. No new replies allowed.