Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active November 13, 2018 16:15
Show Gist options
  • Select an option

  • Save mattn/5770645ef52c69584046a1f9ffa6259a to your computer and use it in GitHub Desktop.

Select an option

Save mattn/5770645ef52c69584046a1f9ffa6259a to your computer and use it in GitHub Desktop.
SRCS = \
pow.c
OBJS = $(subst .c,.o,$(SRCS))
CFLAGS = -fPIC
LIBS = -shared -lm -ldl
TARGET = libpow.so
.SUFFIXES: .c .o
all : $(TARGET)
$(TARGET) : $(OBJS)
gcc -o $@ $(OBJS) $(LIBS)
.c.o :
gcc -c $(CFLAGS) -I. $< -o $@
clean :
rm -f *.o $(TARGET)
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdlib.h>
#include <math.h>
static double (*___pow)(double, double);
__attribute__((constructor))
double
wrap_pow() {
___pow = dlsym(RTLD_NEXT, "pow");
}
double
pow(double x, double y) {
system("aplay pow.wav 2> /dev/null");
return ___pow(x, y);
}
#include <stdio.h>
#include <math.h>
int
main(int argc, char* argv[]) {
printf("pow(4, 2) = %f\n", pow(4, 2));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment