C++ .gitignore template
The C++ template covers the same compiler and linker output as C, plus the files CMake generates during configuration, one of which is a rule you may need to remove.
At a glance
- Category
- Language
- Patterns in this template
- 28 rules
- Directories ignored
- 3 of the 28 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes. The dSYM rule aside, nothing here touches file manager or editor noise.
- Typical companions
- An IDE template, since CLion, Visual Studio and Xcode each leave a different trail.
In detail
Everything the C template covers applies here too: object files, static and shared libraries, executables and debug symbols. C++ adds nothing new to that list, because the difference between the two languages disappears by the time the linker runs. What C++ projects add in practice is a build system, and that is where the extra rules come from.
CMakeCache.txt is the single most important addition. It records every configuration variable resolved during the configure step, including the absolute path to the compiler and to the source tree on the machine that ran it. Copying it to another machine produces confusing failures that look like compiler bugs, which is why a committed cache is worse than no cache at all.
CMakeFiles is the directory of generated build rules and compiler feature probes that sits beside the cache, and cmake_install.cmake is the generated install script. cmake-build-* matches the directory naming convention that CLion uses for its configurations, so a project opened in that editor will produce cmake-build-debug and cmake-build-release without anyone asking for them.
The Makefile rule is the one to think about. It exists because CMake generates a Makefile in the build directory, and generated files should not be tracked. But plenty of C++ projects also keep a hand written top level Makefile as a convenience wrapper, and this rule ignores that file too, with no warning. If your Makefile is something you wrote, either remove the line or add a negation for the exact path.
The safer arrangement, and the one most CMake projects settle on, is to configure out of source into a directory called build and ignore that directory wholesale. Then none of the individual CMake rules matter, and a hand written Makefile at the root stays tracked because nothing is trying to match it by name.
Examples
*.o
*.obj
*.elf
*.ilk
*.map
*.exp
*.gch
*.pch
*.lib
*.a
*.la
*.lo
*.dll
*.so
*.so.*
*.dylib
*.exe
*.out
*.app
*.dSYM/
*.su
*.idb
*.pdb
cmake-build-*/
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
Makefile28 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- Makefile as a rule ignores a hand written Makefile at the repository root as readily as a generated one. Remove the line or negate the specific path with !/Makefile.
- cmake-build-* only matches the CLion naming convention. A build directory called out or _build is not covered by anything in this template.
- A negation cannot rescue a file inside an ignored directory. If you ignore build/ and then write !build/keep.txt, git never descends into build at all, so the negation is never consulted.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →C .gitignore template
What the C .gitignore template covers: object files, static and shared libraries, linker maps, debug symbols and the patterns that swallow a vendored binary.
Rust .gitignore template
The Rust template ignores the target directory and leaves Cargo.lock tracked. Whether that is right depends on whether you ship a binary or a library.
Unreal Engine .gitignore template
The Unreal template ignores compiled binaries, the intermediate build tree, the derived data cache and the saved directory, plus generated project files.
C# .gitignore template
Why the C# template writes [Bb]in instead of bin, what the NuGet and Visual Studio rules cover, and which per developer files it keeps out of the repository.
Ruby .gitignore template
The Ruby template anchors most rules to the repository root with a leading slash, and ignores Gemfile.lock, which is right for a gem and wrong for an app.
PHP .gitignore template
The PHP template ignores the vendor directory and the .env file, and also ignores composer.lock, which most application projects should commit instead.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS