Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active May 22, 2023 21:00
Show Gist options
  • Save opsJson/5f96754dacf79011afcf7a019942dd84 to your computer and use it in GitHub Desktop.
Save opsJson/5f96754dacf79011afcf7a019942dd84 to your computer and use it in GitHub Desktop.
Automatically rebuild your program when you run it
#ifndef _AUTO_BUILD_H_
#define _AUTO_BUILD_H_
#include <stdlib.h>
#ifndef AUTOBUILD_TARGET
#ifdef _WIN32
#define AUTOBUILD_TARGET "autobuild.exe"
#else
#define AUTOBUILD_TARGET "./autobuild"
#endif
#endif
int autobuild_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
int __MAIN__();
int main(int argc, char **argv) {
if (autobuild_strcmp(argv[0], AUTOBUILD_TARGET) != 0) {
system(BUILD_COMMAND);
system(AUTOBUILD_TARGET);
exit(0);
}
return __MAIN__(argc, argv);
}
#define main __MAIN__
#endif /* _AUTO_BUILD_H_ */
/*///////////////////////////////////
Testing:
///////////////////////////////////*/
//make your build system output "autobuild"
//any change in BUILD_COMMAND needs manual compilation
//for any other changes in the program, just run the program again, it will build itself
#define BUILD_COMMAND "echo \"running my build system\" && gcc main.c -o autobuild"
#include "autobuild.h"
#include <stdio.h>
int main(void) {
int n = 0;
printf("Change this number and reopen the program: %i\n", n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment