This software is unfinished
A drop-in replacement for clang
that adds compile-time code execution to C.
ccomptime
allows you to run C code at compile time using comptime
, inline_comptime
directives. The code executes during compilation and can generate code, files, and anything really. You keep all the footguns C gives you even during compilation time!
cc nob.c -o nob
./nob
Prepend your clang command with the ccomptime
cli.
Thats it, ccomptime will "highjack" the compilation process
./ccomptime clang -o program test/main.c
Compile-time execution:
comptime {
printf("This runs at compile time!\n");
}
#include "ccomptime.h"
void generate_file() {
FILE *f = fopen("generated.h", "w");
fprintf(f, "#define MAGIC_NUMBER %d\n", 42);
fclose(f);
}
long long fibo(long long n) {
if (n <= 1)
return n;
long long a = 0, b = 1, c;
for (long long i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
comptime { generate_file(); }
int main() {
long long res = comptime_inline_infer(fibo(34 + 35)); // value will be computed at compile time and inlined here
printf("Res is %llu\n", res);
return 0;
}
The compile-time code runs during compilation, generating generated.h
before the final binary is built.
- Preprocesses source files to find
comptime
blocks - Extracts compile-time code into a separate "runner" program
- Compiles and executes the runner to generate replacements
- Rewrites the original source with results
- Compiles the final program with regular clang
While several languages and tools offer compile-time execution, ccomptime
is unique in bringing full compile-time code execution to C:
- Zig's
comptime
- Built-in compile-time execution with similar capabilities, but requires using Zig - C++
constexpr
- Compile-time function evaluation, but limited to pure functions (no I/O) - Template metaprogramming - C++ templates can generate code, but with complex syntax and limited scope
- Build system generators - Tools like CMake or custom scripts, but separate from compilation
- True compile-time execution in C - Full C language access during compilation
- I/O capabilities - Can generate files, network requests, or any operation (unlike
constexpr
) - Drop-in replacement - Works with existing C codebases without language changes
- Integrated compilation - Part of the compilation process, not a separate build step