Site icon Mariusz Bartosik's website

Directory Structure for a C++ Project

Project directory

When you work on some bigger project, it is important to properly organize its directory structure. Most of the time, you have to prepare project directory structure yourself, as an IDE will create only a basic solution for you.

For example, Visual Studio will create folders named “Debug” and “Release” (just like the current active configuration) with the output executable file in the solution directory, and a similar pair inside sources directory for .obj files. This is a very simple approach, and putting object files inside a folder containing sources is not really version-control-system-friendly. Each time you want to commit some changes in the source files directory, new .obj files in “Debug” or “Release” subfolders are also detected, so you have to ignore these messages or create permanent .gitignore rule for them (if you use Git).

Default directory layout in Visual Studio. Some files omitted for brevity.

Projects often consists of additional data files, documentation and so on. Putting it all together into one directory would create a real mess. Keeping them outside a project directory, in a different place on a hard disk, can be problematic too, especially when you want to easily distribute the project to other users. No one likes to get error messages such as File K:\!data\gfx\shaders\post\fast-blur.cgfx not found!. Ideally, the project should be self-contained, and easy to build.

There are no official C/C++ project directory layout directives, so here’s just one possible solution:

Custom directory layout

Additional notes

Extended objects directory

..and created executables can be named as:

project.exe
project_d.exe
project_x64.exe
project_x64_d.exe

Extended projects directory

The downside of this is that the project will refer to source files like ../../src/main.cpp. Paths that go up and down through folder structure are not the most readable ones, but this is still much better solution than using absolute paths.

# skip binary, temporary and user-specific files

[Bb]in/
[Oo]bj/
*.suo
*.sdf
*.user
*.opendb

Also, check out a collection of useful .gitignore files for different languages and technologies.

Summary

Keeping things organized is important, but there is no one-size-fits-all solution. For other types of projects, a different directory layout may be more suitable. For example, in case of a library project, the “bin” folder should be renamed to “lib”, and a separate “include” directory will allow an easy deployment of the package to the end user.

Also, remember to not over-engineer your project’s structure. You probably don’t need too many levels of subdirectories and do not want to spend a day devising “the perfect structure” suitable for all types of projects. Keep it simple and functional.

Exit mobile version