-
Notifications
You must be signed in to change notification settings - Fork 0
Preprocessor
Derek Snider edited this page May 19, 2026
·
1 revision
madc implements C preprocessor directives at the lexer level.
#define MAX 100
#define PI 3.14159
#define MSG "hello"
cout << MAX << endl; // 100
cout << PI << endl; // 3.14159
#undef MAX
// MAX is no longer definedDefines expand by pushing the value back into the source stream for re-tokenization — they can expand to any valid token sequence.
#ifdef FEATURE_X
// compiled only if FEATURE_X is defined
#endif
#ifndef FEATURE_Y
// compiled only if FEATURE_Y is NOT defined
#endif
#if defined(FEATURE_X) && !defined(FEATURE_Y)
// boolean expressions supported
#endif
#if 1
// always compiled
#endif
#if 0
// never compiled (useful for commenting out blocks)
#endif#ifdef LINUX
// Linux-specific code
#elif defined(MACOS)
// macOS-specific code
#else
// fallback code
#endifNested conditionals are fully supported.
#include "myfile.mad" // quotes: filesystem (relative to current file)
#include <stdio.h> // angle brackets: embedded headers first, then filesystemSee Multi-file Projects and Embedded Headers.
Load a shared library as a namespace:
#load "libfoo.so" as foo;Opens the library via dlopen with RTLD_LAZY | RTLD_GLOBAL. Functions become available as foo::function_name() via dlsym on first use.
RTLD_GLOBAL makes loaded symbols globally visible, so functions can also be called without the namespace prefix via the dlsym fallback.
Controls struct field alignment:
#pragma pack(push, 1)
struct packed_header {
char magic;
int32_t size;
}; // 5 bytes, no padding
#pragma pack(pop)Sets namespace precedence:
#pragma prefer rust, php, cSave and restore macro definitions:
#define FOO 42
#pragma push_macro("FOO")
#undef FOO
// FOO is undefined here
#pragma pop_macro("FOO")
// FOO is 42 again| Macro | Value |
|---|---|
__FILE__ |
Current source filename |
__LINE__ |
Current line number |
These work inside function-like macros too.
- Embedded Headers — built-in standard headers
- C23 Features — modern C standard coverage
- ← Back to Home