You can compile and link everything statically, as usual (you may use g++, clang++ or any other command line C++ compiler):
g++ main.cpp ReloadableFunction.cpp -o App.exe
Or, alternatively, you can enable hot reload by defining the macro HOT_RELOAD.
First compile the static files:
g++ -DHOT_RELOAD main.cpp FunctionLoader.cpp -o App.exe
Then compile the ReloadableFunction.cpp file, that contains the definition of the DrawAscii function, to the DLL DrawAscii.dll (the DLL must have the same name as the function inside of it):
g++ -DHOT_RELOAD -shared ReloadableFunction.cpp -o DrawAscii.dll
Run App.exe. You can then make any modifications to the DrawAscii function and recompile it. The changes will take place seamlessly, while the program is still running.
CppHotReload is a minimalist library that enables automatic hot reloading of C++ functions. It provides two macros: DEF_RELOADABLE_FUNCTION and IMPL_RELOADABLE_FUNCTION, that can be used to define and implement hot reloadable functions. Hot reloadable functions can be compiled either statically or as reloadable DLLs, depending on whether the HOT_RELOAD macro is defined.
When HOT_RELOAD is not defined, all functions are compiled normally into the executable, allowing full use of compiler and linker optimizations.
When HOT_RELOAD is defined, any functions declared and implemented with the macros above are compiled as DLLs. Each reloadable function must be placed in its own source file and compiled into its own DLL. Whenever one of these DLLs is recompiled, the system automatically detects the change, unloads the previous version of the function, and seamlessly loads the new one, all while the program continues running, no restart required.
You can freely move, rename, delete, or replace the DrawAscii.dll file while the program is running. Whenever a new file named DrawAscii.dll appears in the project folder, the system automatically triggers a function reload.
This mechanism works by maintaining an internal object that stores the function pointer, the DLL handle, and a 64-bit hash of the DLL’s last write time. The first time a hot-reloadable function is called, the program copies the DLL into the gen folder (creating it if necessary), then loads the handle and function pointer from that copy, while also storing the hash of the original file’s last write time.
Before each subsequent function call, the system checks whether any project DLLs have been updated. If a new version is detected (based on a changed last write time) it unloads the previous DLL handle, deletes the old copy, duplicates the updated DLL, loads it, and replaces the function pointer.
- Support multiple hot reloadable function declarations and definitions on the same file.
- Spawn a separate thread that continuously checks for updated DLL files independently of the main thread, to avoid the overhead when calling hot reloadable functions.
- Add support for Linux .so files.
Feel free to contribute or suggest any new features!
Questions and feedback are welcome. Please send them to my email diego.quintanilha@hotmail.com.