PCPP is a pure Python implementation of a C99 preprocessor. It's designed to preprocess C and C++ source code, handling directives like `#include`, `#define`, and conditional compilation. A key use case is for processing header-only C++ libraries into single-file includes and for integration with documentation tools like Doxygen. The library is actively maintained, though with an infrequent release cadence, with the latest significant update in late 2021.
pip install pcppVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate the `Preprocessor` class, define custom macros, feed it C source code, and retrieve the preprocessed output. The `parse` method takes a file-like object, and `stream` returns an iterator of tokens which can be joined to form the final string.
If relying on `#line` directives for debugging or tools, ensure your build system or downstream tools are aware of and correctly interpret relative paths. Adjust expectations for `__FILE__` and `__LINE__` macros accordingly.
When using the command-line tool, use the `--compress` option. When using the API, check for a corresponding `compress` attribute or method on the `Preprocessor` instance (consult the documentation for exact API usage).
Use the `passthru_magic_macros` option when initializing `Preprocessor` or the `--passthru-magic-macros` command-line argument if you want these macros to remain unexpanded in the output.
Ensure the library is installed using pip: `pip install pcpp`
Add the directory containing the header file to the preprocessor's include paths using `p.add_path('/path/to/headers')` or the `-I` command-line option. Relative paths are often resolved from the current working directory, but explicit paths are robust.Review the expression for correct C99 preprocessor syntax. Ensure all macros used in the expression are defined or handle undefined macros gracefully. PCPP v1.30 improved expression evaluation with a yacc-based parser, fixing many issues from older `eval()`-based approaches.