cxxheaderparser
raw JSON → 1.7.0 verified Mon Apr 27 auth: no python
A modern, Python-based parser for C++ header files. It can parse C++ headers into an AST and optionally generate a JSON representation. Version 1.7.0 supports Python >=3.6. Releases are occasional, with active maintenance.
pip install cxxheaderparser Common errors
error ModuleNotFoundError: No module named 'cxxheaderparser' ↓
cause Package not installed or wrong environment.
fix
Run
pip install cxxheaderparser error cxxheaderparser.errors.ParseError: ... ↓
cause The input C++ code has syntax that the parser cannot handle (e.g., macro, C++20 feature).
fix
Simplify the header, preprocess with cpp, or check supported C++ version.
error AttributeError: module 'cxxheaderparser' has no attribute 'parse' ↓
cause Using old import path from version 0.x.
fix
Replace
from cxxheaderparser import parse with from cxxheaderparser import parse_file or parse_str. Warnings
gotcha The parser expects valid C++ syntax. Preprocessor macros (#define, #ifdef) are not resolved and may cause parse errors or unexpected AST. Use a preprocessor or pass already-preprocessed code. ↓
fix Run your code through a preprocessor (e.g., cpp) before parsing, or avoid macro-heavy headers.
gotcha The library does not fully support C++20 concepts, modules, or some newer features. Parsing may fail on modern templates or requires. ↓
fix Stick to C++17 or earlier for reliable parsing. Consider libclang-based alternatives if full standard support is needed.
breaking Between versions 0.x and 1.x, the API changed significantly. The old `cxxheaderparser.parse` module was restructured into `cxxheaderparser` package with new import paths. ↓
fix Use `from cxxheaderparser import parse_file` instead of `from cxxheaderparser import parse`.
Imports
- parse_file
from cxxheaderparser import parse_file - parse_str
from cxxheaderparser import parse_str - ParserOptions
from cxxheaderparser.options import ParserOptions - ParsingResult
from cxxheaderparser.types import ParsingResult
Quickstart
from cxxheaderparser import parse_str
code = '''
int add(int a, int b);
'''
result = parse_str(code)
print(result.namespace.name_qual)
print(result.functions[0].name) # 'add'