mc:用於生成標頭檔的 C 預處理器前置工具
mc- A pre-preprocessor for C for header files generation
mc(meta-c)是一款 C 預處理器前置工具,可透過自訂註解宏在 .c 檔案中編寫標頭內容,並生成 .h 檔案。每個 .c 檔案頂部都須有 @pub 宏,宏內內容會寫入標頭檔;生成的標頭檔模板均會在頂部加入 #pragma once。
mc
mc stands for meta-c:
A pre-preprocessor for C.
It generates .h header files for .c files using custom macro syntax.
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!");
}
- 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;
}
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//@endwill go to the .h header file). - Every generated header file template includes
#pragma onceat 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
License
MIT
來源:DEV Community · dev.to