https://github.com/agagniere/blackmagic
C preprocessor tricks, explained
https://github.com/agagniere/blackmagic
c preprocessor tutorial zig-package
Last synced: 4 months ago
JSON representation
C preprocessor tricks, explained
- Host: GitHub
- URL: https://github.com/agagniere/blackmagic
- Owner: agagniere
- License: mit
- Created: 2023-08-19T19:43:47.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-07-29T11:41:15.000Z (6 months ago)
- Last Synced: 2025-07-29T13:54:59.507Z (6 months ago)
- Topics: c, preprocessor, tutorial, zig-package
- Language: C
- Homepage: https://agagniere.github.io/blackmagic/
- Size: 222 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Black Magic
*`impossible` is not a C keyword*
**Learn about C preprocessor tricks**
This book delves into techniques to generate C code with the preprocessor,
starting with a reminder about what the preprocessor is and how it is integrated in the C compilation pipeline,
then building up increasingly complex tools anyone can use in their C projects.
[Read it online !](https://agagniere.github.io/blackmagic/)
## Tools
Add this project as a dependency to your C project today (using Zig, Nix or Conan) to get:
- ANSI color codes as a string literal by writing `COLOR(UNDERLINED, BOLD, RED)` [`blackmagic/color.h`](include/blackmagic/color.h)
- Convert enums to/from strings [`blackmagic/enum.h`](include/blackmagic/enum.h)
- Logs with a compile-time configurable format and level [`blackmagic/log.h`](include/blackmagic/log.h). Possible formats include:
- colored console
- JSON
- Markdown table
- custom callback
## Roadmap
I would like to explain the following tricks in the book:
- [x] Logging
- [ ] Default arguments
- [ ] Function overloading
- [ ] Enum to/from string
- [ ] Serialization / Deserialization of a structure
- [ ] Unit-tests library
## Extras
- Sphinx directive to show C code before and after preprocessing
- Custom pygment lexer to color the code the way the preprocessor sees it
## Use it
### With Zig build as your C package manager
Add the dependency in your `build.zig.zon` by running the following command:
```shell
zig fetch --save git+https://github.com/agagniere/blackmagic#master
```
Then, in your `build.zig`:
```zig
const blackmagic = b.dependency("blackmagic", .{});
// wherever needed:
exe.addIncludePath(blackmagic.path("include"));
```
### With Conan
Add the dependency in your `conanfile.txt`
```toml
[requires]
blackmagic/0.3
```