Created
September 18, 2012 22:58
-
-
Save mzp/3746562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get() { | |
puts("I'm get function of libbar"); | |
return 100000; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get() { | |
puts("I'm get function of libfoo"); | |
return 42; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
(cd libfoo && make) | |
(cd libbar && make) | |
gcc -o loader test_dyld.c | |
./loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get(); | |
int get_answer(){ | |
puts("I'm libbar and call get()"); | |
return get() + 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get(); | |
int get_answer(){ | |
puts("I'm libfoo and call get()"); | |
return get() + 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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