Demonstrates hot reloading functions that call other functions across multiple files.
Three files work together:
utils.cpp- Helper functions (getMultiplier(),getAdder())calculator.cpp- The file we'll hot reload (calls functions from utils.cpp)main.cpp- Main program loop
calculator.cpp line 8 should multiply by 20, but only multiplies by 10.
// Current (wrong):
return x * getMultiplier(); // getMultiplier() returns 10, so 5 * 10 = 50
// Fix:
return x * getMultiplier() * 2; // 5 * 10 * 2 = 100 ✓# Build (note the -rdynamic flag!)
./build.sh
# Debug
lldb ./myapp
(lldb) command script import ../../src/hotreload.py
(lldb) b main.cpp:27
(lldb) runThe program will print:
calculate(5) = 50 (expected: 100)
Fix the bug and run: (lldb) hotreload calculator.cpp
Edit calculator.cpp line 8:
return x * getMultiplier() * 2; // Add the * 2Save the file.
(lldb) hotreload calculator.cpp
(lldb) continue
You should see:
✓ Patched 2/2 functions
calculate(5) = 100 (expected: 100)
SUCCESS! Hot reload worked!
- ✅ Inter-file dependencies -
calculator.cppcallsgetMultiplier()fromutils.cpp - ✅ Runtime symbol resolution - Uses
RTLD_GLOBAL+-rdynamic - ✅ Multiple functions - Hot reloads both
calculate()andcomplexCalculation()
This example requires the binary to be built with -rdynamic:
clang++ -rdynamic -g -O0 main.cpp utils.cpp calculator.cpp -o myappWhy? The -rdynamic flag exports symbols to the dynamic symbol table so the hot-reloaded .so can find functions from the original binary at runtime.
Without it, you'll see:
✗ dlopen() failed: undefined symbol: _Z13getMultiplierv
⚠ HINT: Your binary may not have been built with -rdynamic
main.cpp- Main programcalculator.cpp- Hot reload this fileutils.cpp- Helper functionsutils.h- Header filebuild.sh- Build script (includes-rdynamic)compile_commands.json- Compiler flags