Skip to content

Instantly share code, notes, and snippets.

@alexcrichton

alexcrichton/Makefile Secret

Last active Aug 29, 2015
Embed
What would you like to do?
#include <assert.h>
void free(void *ptr) {
assert(0);
}
void foo(void *ptr) {
free(ptr);
}
#include <dlfcn.h>
#include <assert.h>
#include <stdlib.h>
int main() {
void *lib = dlopen("libfoo." EXT, RTLD_NOW);
assert(lib);
void* ptr = malloc(8);
void (*foo_fn)(void*) = dlsym(lib, "foo");
assert(foo_fn);
foo_fn(ptr);
}
ifeq ($(shell uname),Darwin)
all:
$(CC) -dynamiclib foo.c -o libfoo.dylib
$(CC) main.c -o main -DEXT='"dylib"'
./main
else
all:
$(CC) -fPIC -shared foo.c -o libfoo.dylib
$(CC) main.c -o main -ldl -DEXT='"so"'
./main
endif
$ make
cc -fPIC -shared foo.c -o libfoo.so
cc main.c -o main -ldl -DEXT='"so"'
./main
$ make
cc -dynamiclib foo.c -o libfoo.dylib
cc main.c -o main -DEXT='"dylib"'
./main
Assertion failed: (0), function free, file foo.c, line 4.
make: *** [all] Abort trap: 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment