Skip to content

Instantly share code, notes, and snippets.

@xealits
Last active October 2, 2025 18:53
Show Gist options
  • Save xealits/9365b19882ba411711e39e7d723074e4 to your computer and use it in GitHub Desktop.
Save xealits/9365b19882ba411711e39e7d723074e4 to your computer and use it in GitHub Desktop.
C hotreload/figwheel in Python
#!/usr/bin/env python
import ctypes
from ctypes import cdll
cur_x = 10
libpath = "./some_lib.so"
lib = cdll.LoadLibrary(libpath)
print(f"loaded {lib}")
# https://stackoverflow.com/a/62021495/1420489
def ctypesCloseLibrary(lib):
dlclose_func = ctypes.CDLL(None).dlclose
dlclose_func.argtypes = [ctypes.c_void_p]
dlclose_func.restype = ctypes.c_int
dlclose_func(lib._handle)
def reloadLibrary(lib):
name = lib._name
ctypesCloseLibrary(lib)
return cdll.LoadLibrary(name)
while True:
inp = input("> ")
inp = inp.strip().upper()
if inp == "X":
x = lib.foo(cur_x)
print(x)
elif inp == "L":
lib = reloadLibrary(lib)
print(f"reloaded {lib}")
else:
print(f"unknown input {inp}")
@xealits
Copy link
Author

xealits commented Oct 2, 2025

just to write it down:

# Makefile
%.o: %.c
	gcc -c -fPIC $< -o $@

%.so: %.o
	gcc $< -shared -o $@

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment