Skip to content

Instantly share code, notes, and snippets.

@mzp
Created September 18, 2012 22:58
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 mzp/3746562 to your computer and use it in GitHub Desktop.
Save mzp/3746562 to your computer and use it in GitHub Desktop.
int get() {
puts("I'm get function of libbar");
return 100000;
}
int get() {
puts("I'm get function of libfoo");
return 42;
}
#!/bin/sh
(cd libfoo && make)
(cd libbar && make)
gcc -o loader test_dyld.c
./loader
int get();
int get_answer(){
puts("I'm libbar and call get()");
return get() + 1;
}
int get();
int get_answer(){
puts("I'm libfoo and call get()");
return get() + 1;
}
libbar: main.o common.o
gcc -dynamic -bundle -g -fPIC -o libbar.bundle $^ -Wl,-flat_namespace -fPIC
%.o : %.c
gcc -g -fPIC -c -o $@ $^
clean:
rm -f *.o *.bundle
libfoo: main.o common.o
gcc -dynamic -bundle -g -fPIC -o libfoo.bundle $^ -Wl,-flat_namespace -fPIC
%.o : %.c
gcc -g -fPIC -c -o $@ $^
clean:
rm -f *.o *.bundle
#include <dlfcn.h>
#include <stdio.h>
int load_and_call(const char* lib){
void* handle = dlopen(lib, RTLD_GLOBAL | RTLD_LAZY);
printf("error=%s\n", dlerror());
int(*f)(void) = dlsym(handle, "get_answer");
if(f != NULL){ printf("ret = %d\n", f()); }
}
int main(int argc, char** argv){
int i;
printf("loading libfoo. and works well");
load_and_call("libfoo/libfoo.bundle");
printf("loading libbar. and binding other function");
load_and_call("libbar/libbar.bundle");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment