Skip to main content

Headercount – Count which header is included how many times

Headercount is a small debugging script that is meant to help developers who are trying to cut down compile times in a medium-sized C++ project.

Headercount recursively searches a project for #include and prints statistics about which header was included how many times. This indicates which headers are depended on the most. With this information, a developer knows where to start with methods such as the PIMPL idiom, forward declarations, reduction of templatization, etc.

Installation

Headercount is a Python package based on Setuptools. It requires Python 3.4 or higher. It has no dependencies beyond the Python standard library. The easiest way to install it is via pip:

pip install headercount

You can also install Headercount manually by cloning this repository:

git clone https://git.sr.ht/~brokenpen/headercount
cd headercount
pip install .

Working Principle

Headercount works on a best-effort basis and may give incorrect results when faced with strange include pattern. Contributions to improve the algorithm are highly welcome!

For any given input file, Headercount checks whether it is a C/C++ file based on its file name suffix. Non-matching files are ignored. Directories are ignored as well, unless --recursive is passed; if it is, directories are searched recursively for C/C++ files.

Any accepted file is searched for #include <...> and #include "..." directives. Any found directive is counted towards the final tally. Additionally, Headercount attempts to match each include directive to one of its input files, based on the input files' base names. If such a match succeeds, the includes of the matched files are added to the list of includes found in the originally searched file.

This means that a header file is not searched for additional includes if it is not specified on the command line. This helps make the behavior of Headercount predictable.

Finally, the total numbers are tallied for each include across all input files.

The following arguments allow to modify this default behavior:

  • --no-headers: Only source files (as termined by the file name suffix) are used to form the final tally. Header files are only used to determine recursive dependencies. This is useful for many developers, who are only interested in the impact of an include on compilation times.

  • --no-system: The final tally excludes all #include <...>-style directives. This is useful if the developer is only interested in intra-project dependencies.

  • --direct-only: No recursive search is done. The tally only counts literal #include directives.

  • --allow-duplicates: If a file is included multiple times (literally or due to indirect includes), all occurrences are counted.

Contributing

Headercount is only a tiny project made by a tiny guy. Any help is highly encouraged. If you wish to contribute, simply open an issue or send a pull request.

Headercount supports Python 3.4, C11, and C++11 and onwards. If it does not, it is a bug and should be reported. It uses Pep8 and uses PyLint for style checking. Before sending a pull request, please make sure that these tools don't give any warnings.

Metadata

Release files for headercount 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for headercount 1.0.1
File Size Uploaded
headercount-1.0.1.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for headercount 1.0.1
File Interpreter ABI Platform
headercount-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.2 kB

Release files / headercount-1.0.1.tar.gz

Download URL headercount-1.0.1.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
31d33037b5580b5d9446f485ec06c83199f0296808d8d00fc0c1dd38d1820170
BLAKE2b-256 checksum
How to use checksums
c1436fabe3833f39fda8a80ca55e839b20c95728ce2540f24c45fb995c6c20f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.10

Release files / headercount-1.0.1-py3-none-any.whl

Download URL headercount-1.0.1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
559f63e87d0f6243c7aa652a3f8c96de2d059905b8bd86c8296a000dd8a45f1e
BLAKE2b-256 checksum
How to use checksums
2a27cfff508fd1e2daeffedf05b41f046f66e07736e4365ae2d983853fa128d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.10

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page