Files
cmeta/README.md
2026-05-22 00:57:02 +02:00

78 lines
1.4 KiB
Markdown

# cmeta
> [!WARNING]
> Work in progress. This is not finished at all, I'm just experimenting.
Single Heaer Meta Programming Header Library for C. The same header for both codegen and runtime.
## Current limitations
- Only parses `typedef struct { ... } type_name;`
- Will generate colliding definitions in case of types with the same name
- Parsing expects more or less correct code, if you forget a semi it will generating bad code
## Quick start
1. Create `main.c`
```c
typedef struct {
int int_field;
const char *const_char_star_field;
} My_Struct;
int main(void) {
return 0;
}
```
2. Create `cmeta.c`
You only have to call `process_file` with a file to generate type infos etc in `cmeta.h`.
```c
#define CMETA_COMPTIME
#include "cmeta.h"
int main(void) {
if (!process_file("./main.c")) return 1;
return 0;
}
```
3. Build and run cmeta
```console
$ gcc cmeta.c -o cmeta
$ ./cmeta
```
4. Enjoy!
You can now inspect your types!
```c
#include "cmeta.h"
typedef struct {
int int_field;
const char* const_char_star_field;
} My_Struct;
int main(void) {
printf("My_Struct.name = %s\n", my_struct_info.name);
printf("My_Struct.fields_count = %zu\n", my_struct_info.fields_count);
return 0;
}
```
```console
$ gcc main.c -o main
$ ./main
My_Struct.name = My_Struct
My_Struct.fields_count = 2
```
> [!INFO]
> It runs `gcc -E`, so it handles macros etc.