Last active
December 18, 2022 07:45
-
-
Save ziap/50a24f552169ef77963c009871a93496 to your computer and use it in GitHub Desktop.
Automatically recompile c++ file when the source changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdlib> | |
| #include <filesystem> | |
| #ifndef LAST_BUILD | |
| #define LAST_BUILD 0 | |
| #endif | |
| // USAGE: | |
| // ```cpp | |
| // int main(int argc, char** argv) { | |
| // REBUILD(argc, argv); | |
| // // actual work | |
| // return 0; | |
| // } | |
| // ``` | |
| #define REBUILD(argc, argv) \ | |
| { \ | |
| auto last_write = std::filesystem::last_write_time(__FILE__); \ | |
| auto last_write_unix = last_write.time_since_epoch().count(); \ | |
| if (last_write_unix != LAST_BUILD) { \ | |
| std::stringstream ss; \ | |
| ss << "c++ " << __FILE__ << " -o " << argv[0] << " -std=c++" \ | |
| << __cplusplus / 100 % 100 << " -DLAST_BUILD=" << last_write_unix \ | |
| << '\n'; \ | |
| if (std::system(ss.str().c_str())) \ | |
| return 1; \ | |
| std::system(argv[0]); \ | |
| return 0; \ | |
| } \ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment