CPP-CLOC has now shifted from active development to maintenance mode.
This project taught me more about C++ than any tutorial ever could. The original goals were:
- Re-implement
clocin a performant, statically typed language (the Python prototype didnβt cut it for performance). - Learn how to structure and build a serious C++ project from scratch.
Both goals have been largely achieved. While CPP-CLOC is not βfeature-completeβ in the absolute sense, it is functionally solid for its intended purpose, and further polishing or adding new features would likely become an endless process. Itβs now time to let the project rest.
β‘ Bug fixes and minor stability updates may still happen, but no new features are planned.
- πβ¨ CPP-CLOC - C++ Implementation of CLOC
β οΈ Maintenance Mode:- π Table of Contents
- Current Version
- π Overview
- β‘ Quick Install
- π οΈ Build Modes
- π» Usage
- π Examples
- βοΈ Features
- Tests
- π¦ Dependencies
β οΈ Known Limitations- π§ Design Philosophy
- π Project Structure
- π§βπ» Development Guide
- π€ Contributing
- LICENSE
- π¬ Author
v0.6.3 β Added release build mode with -O3 compiler optimization via Makefile. Debug build remains default. See CHANGELOG for details.
CPP-CLOC is a minimal C++ implementation of CLOC (Count Lines of Code) for multiple programming languages:
- C, C++, C/C++ headers
- Java
- Python
- HTML, MarkDown, CSS
- JavaScript / TypeScript
- Bash / PowerShell / Batch
- Unknown (Not analyzed)
It provides fast, cross-platform analysis of code files, counting:
- Lines of code
- Comment lines
- Blank lines
- Total lines
β‘ Designed for speed and simplicity, written in modern C++.
β οΈ Requires C++17 compatible compiler and includesargparse.hppheader
git clone https://github.com/journeycodesayush/cpp-cloc.git
cd cpp-cloc
make
make RELEASE=1 # (optional) build optimized binary
./cloc_cpp <filename>git clone https://github.com/journeycodesayush/cpp-cloc.git
cd cpp-cloc
mkdir build
make RELEASE=1 # (optional) build optimized binary
.\cloc_cpp.exe <filename>CPP-CLOC supports two build modes via Makefile:
Includes debug symbols and no optimization:
makeEnables compiler optimizations for improved performance:
make RELEASE=1β‘ Use release builds for faster execution on large codebases.
For better performance on large projects, build with
make RELEASE=1to enable compiler optimizations.
Run cpp-cloc on a directory or single file:
cloc_cpp src/Optional output format flags (mutually exclusive):
cloc_cpp src/ --json
cloc_cpp src/ --csv
cloc_cpp src/ --xml
# Scan a directory and save results to a file
cloc_cpp ./src --output=results.txt
# Select output format and save to a file
cloc_cpp ./src --json --output=results.json
# Exclude directories by name
cloc_cpp ./src --exclude-dir=build,testIf no flag is provided, output is printed in the default table format.
Output includes:
- Number of files analyzed
- Total lines of code
- Total comment lines
- Total blank lines
- Total lines overall
Languages supported:
| Language | Extension(s) | Comment Style |
|---|---|---|
| C/C++ | .c, .cpp, .h |
//, /* */ |
| Java | .java |
//, /* */ |
| Python | .py |
#, """ """ |
| HTML | .html |
<!-- --> |
| MarkDown | .md |
<!-- --> |
| CSS | .css |
/* */ |
| JS/TS | .js, .ts |
//, /* */ |
| Bash | .sh |
# |
| PowerShell | .ps1 |
# |
| Batch | .bat |
REM |
Note
Only triple-double-quoted(""" """) strings are recognized for Python
./cloc_cpp ./srcOutput example:
C++ implementation of CLOC
Total time: 0.091 seconds
------------------------------------------------------------------------------
Language Files Code Comments Blank Total
------------------------------------------------------------------------------
C++ 6 372 6 45 423
------------------------------------------------------------------------------
SUM 6 372 6 45 423
------------------------------------------------------------------------------
./cloc_cpp ./src/main.cpp- β Catch2-based analyzer test suite with invariant coverage
- β Supports multiple programming languages
- β Counts code, comments, blank lines, and total lines
- β Handles multi-line comments (/ ... /) and single-line comments
- β Cross-platform (Windows, Linux, macOS)
- β Lightweight and fast
β οΈ Minimal dependency: requiresargparse.hpp(included ininclude/argparse/)
Tests for the analyzer, the heart of CPP-CLOC, are available in tests/test_analyzer.cpp
Run tests with:
make test| Library | License | Notes |
|---|---|---|
argparse.hpp |
MIT | Header-only C++17 library, included in include/argparse/, required to build the main binary |
| Library | License | Notes |
|---|---|---|
Catch2 |
MIT | Header-only, version 2.13.10, vendored in include/catch2/, used for analyzer tests only |
β οΈ All external code is included as-is and is subject to its original license. CPP-CLOC itself is MIT-licensed.
- Multi-line comments inside strings are intentionally treated as comments
printf("/*");
for(size_t i = 0; i <= 5; i++){
printf("%d ",i);
}
printf("*/");is counted as 2 lines of code and 3 lines of comment
- comment markers inside strings are counted as comments
- Does not detect nested comment blocks by design
- Only triple-double-quoted (""" """) strings are treated as comments in Python; single-quote docstrings (''') are not recognized.
- CPP-CLOC follows single-pass text-based analysis; it does not fully parse languages.
- Some counting behaviors are cloc-aligned quirks, not bugs:
- Lines with code + comments are counted as code.
- Comment markers inside strings are treated as comments.
- Embedded languages are not recognized.
- Python docstrings are intentionally treated as comments.
- Contributions may extend supported languages, improve performance, or refactor code, provided analyzer behavior and cloc-style counting semantics remain unchanged.
Important
cpp-cloc intentionally does not distinguish comment markers inside strings, docstrings, or other language constructs, because doing so would require partial parsing and violate its strictly text-based analysis model.
See PHILOSOPHY.md for a detailed guide.
cpp-cloc/
βββ include/ # Header files
βββ src/ # Source files
β βββ analyzer.cpp
β βββ detector.cpp
β βββ list_files.cpp
β βββ middleware.cpp
β βββ print.cpp
β βββ main.cpp
β
βββ tests/ # Catch2 based analyzer tests
βββ Makefile # Build configuration
βββ README.md
βββ LICENSE- Analyzer:
analyzer.cppcontains core logic - Stats:
stats.cppdefines counting structures - String utilities: trimming, stripping whitespace
- Languages supported: extend
comment_syntax.hppand update thedetector.cppandmiddleware.cppto recognize the language
Tip: Add new languages by defining
CommentSyntaxand following the above instruction.
- Fork the repo
git clone https://github.com/journeycodesayush/cpp-cloc.git- Create a branch:
feat/new-languageorfix/bug
git switch -c feat/new-language- Make changes & test locally
- Open a PR with clear description
Follow Angular Commit Message Convention:
feat(analyzer): add support for new language
fix(stats): correct blank line counting
docs(readme): update usage section
chore(build): update Makefile
refactor(analyzer): improve multiline comment detection
MIT License
Copyright (c) 2025 JourneyCodesAyush
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the βSoftwareβ), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Made with β€οΈ by JourneyCodesAyush