DEV Community

csm
csm

Posted on Edited on

mc- A pre-preprocessor for C for header files generation

mc

mc stands for meta-c:
A pre-preprocessor for C.

It generates .h header files for .c files using custom macro syntax.

link to repo

Disclaimer

With mixed feelings of guilt and pride, I want to say that this is a purely vibe-coded app.
But it works exactly the way I wanted it to.

Why

In C, as a beginner, its difficult to keep switching between a .c file and its corresponding .h file just to update declarations. mc solves my problem.

I got the idea from Pascal, where you can write the implementation and interface in the same file.

Why the comments as macros

Its because,by using comments as macros, code editors don't need a new language extension to recognise plain regular c code.
Best of both worlds.

Development Environment

  • Operating System: Linux Mint
  • Go Toolchain

Example

The idea is:
In a single .c file using custom comment macro syntax we write all our .h header content and give the file to mc and it will generate the header file.

  • hello.c:
// Hello module example!

//@pub
void hello();
//@end

#include<stdio.h>

void hello(){
    puts("Hello world!");
}

Enter fullscreen mode Exit fullscreen mode
  • main.c:
// Enven if empty each .c file should have the @pub macro at the top
//@pub
//@end

#include "hello.h"

int main(){
    hello();
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

MC Rules

  • Each .c file should have @pub macro at the top.
  • Before @pub macro whitespace characters and single line comments are allowed.
  • @pub macro can be empty.
  • Anything inside (between the macro comments //@pub //@end will go to the .h header file).
  • Every generated header file template includes #pragma once at the top, even if the @pub macro is empty.

Commands

mc <no args>      -> Print mc version and help
mc -version,  -v  -> Print mc version
mc -help,     -h  -> Print mc commands list
mc -watch,    -w  -> Watch for any file changes and regenerate .h files
mc hi.c main.c    -> Generate .h header files in include/ folder for given .c files
Enter fullscreen mode Exit fullscreen mode

License

MIT

Top comments (0)