Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active July 25, 2020 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/8c816f39fe04d38d83022301872272ea to your computer and use it in GitHub Desktop.
Save harieamjari/8c816f39fe04d38d83022301872272ea to your computer and use it in GitHub Desktop.
example plugin
#include <stdio.h>
#include "main.h"
void some_func(){
burger += 10;
}
#include <stdio.h>
#include <stdlib.h> // for exit()
#include <dlfcn.h>
#include "main.h"
int burger = 3;
int main(){
void (*ptr_func)();
void *handle;
handle = dlopen("./libfunc.so", RTLD_NOW);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
*(void**)(&ptr_func) = dlsym(handle, "some_func");
if (!ptr_func){
fprintf(stderr, "%s\n", dlerror());
dlclose(handle);
exit(1);
}
printf("before ptr_func %d\n", burger);
ptr_func();
printf("after %d\n", burger);
return 0;
}
#ifndef MAIN_H__
#define MAIN_H__
extern int burger;
#endif
all : main libfunc.so
.PHONY : all
main : main.c
$(CC) -rdynamic $^ -o $@
libfunc.so : func.c
$(CC) -shared -fPIC $^ -o $@
.PHONY : clean
clean :
rm main libfunc.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment