Created
June 28, 2025 23:44
-
-
Save TheMatt2/828bea5189923169ffec39f98f4615e4 to your computer and use it in GitHub Desktop.
A demonstrate of dynamic executable loading for Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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