logo-square.png

Good morning! It is still 2020, and the world is literally on fire, so I guess we could all use a distraction.

This article continues the tradition of me getting shamelessly nerd-sniped - once by Pascal about small strings, then again by a twitch viewer about Rust enum sizes.

This time, Ana was handing out free nerdsnipes, so I got in line, and mine was:

How about you teach us how to how reload a dylib whenever the file changes?

And, sure, we can do that.

What's a dylib?

dylib is short for "dynamic library", also called "shared library", "shared object", "DLL", etc.

Let's first look at things that are not dynamic libraries.

We'll start with a C program, using GCC and binutils on Linux.

Say we want to greet many different things, and persons, we might want a greet function:

Shell session$ mkdir greet
$ cd greet/

C code// in `main.c`#include<stdio.h>voidgreet(constchar*name) {
printf("Hello, %s!\\n",name);
}

intmain(void) {
greet("moon");
return 0;
}

Shell session$ gcc -Wall main.c -o main
$ ./main
Hello, moon!

This is not a dynamic library. It's just a function.

We can put that function into another file:

C code// in `greet.c`#include<stdio.h>voidgreet(constchar*name) {
printf("Hello, %s!\\n",name);
}

And compile it into an object (.o) file:

Shell session$ gcc -Wall -c greet.c
$ file greet.o
greet.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Then, from main.c, pinky promise that there will be a function named greet that exists at some point in the future: