Skip to content

Instantly share code, notes, and snippets.

@TheMatt2
Created June 28, 2025 23:44
Show Gist options
  • Save TheMatt2/828bea5189923169ffec39f98f4615e4 to your computer and use it in GitHub Desktop.
Save TheMatt2/828bea5189923169ffec39f98f4615e4 to your computer and use it in GitHub Desktop.
A demonstrate of dynamic executable loading for Linux
// gcc -fPIC -shared hello_so.c -o libhello.so
// gcc hello_dl.c -o hello
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
const char *LIB_SO = "./libhello.so";
int main(void) {
void *handle = dlopen(LIB_SO, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); // clear error status
void (*hello)(void);
hello = dlsym(handle, "hello");
char *error = dlerror();
if (error != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
}
hello();
dlclose(handle);
return 0;
}
#include <stdio.h>
void hello(void) {
char buf[100];
printf("Hello, what is your name?: ");
scanf("%100s", buf);
printf("\nHello %s!\n", buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment