Load mod dynamically

This commit is contained in:
Camden Dixie O'Brien 2024-12-29 12:02:04 +00:00
parent 472dff7d80
commit 86a8a6c5f7
3 changed files with 6 additions and 5 deletions

View File

@ -2,4 +2,4 @@
CFLAGS="-std=c11 -pedantic -Wall -Wextra"
mkdir -p build
clang $CFLAGS -fPIC -shared mod.c -o build/libmod.so
clang $CFLAGS -Lbuild -lmod main.c -o build/demo
clang $CFLAGS main.c -o build/demo

7
main.c
View File

@ -5,13 +5,16 @@
#define _POSIX_C_SOURCE 199309L
#include "mod.h"
#include <dlfcn.h>
#include <stdio.h>
#include <time.h>
typedef const char *(*getmsg_t)(void);
int main(void)
{
void *mod = dlopen("libmod.so", RTLD_NOW);
const getmsg_t getmsg = (getmsg_t)dlsym(mod, "getmsg");
const struct timespec pause = { .tv_nsec = 16666667 };
while (1) {
printf("\33[2K\r%s", getmsg());

2
mod.c
View File

@ -3,8 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "mod.h"
const char *getmsg(void)
{
return "hi";