Skip to content

Instantly share code, notes, and snippets.

@manuelm
Created May 15, 2016 17:34
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 manuelm/43a4fa9dd424b4dcf03bd1d773a0e122 to your computer and use it in GitHub Desktop.
Save manuelm/43a4fa9dd424b4dcf03bd1d773a0e122 to your computer and use it in GitHub Desktop.
diff -Naur missing/runtimelib/runtimelib.c ./runtimelib/runtimelib.c
--- missing/runtimelib/runtimelib.c 1970-01-01 01:00:00.000000000 +0100
+++ ./runtimelib/runtimelib.c 2016-05-15 19:29:27.288102521 +0200
@@ -0,0 +1,4 @@
+int myfct()
+{
+ return 123;
+}
diff -Naur missing/lib/mylib.c ./lib/mylib.c
--- missing/lib/mylib.c 1970-01-01 01:00:00.000000000 +0100
+++ ./lib/mylib.c 2016-05-15 19:29:27.205101566 +0200
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+void load_runtimelib()
+{
+ void *handle = dlopen("libruntimelib.so", RTLD_LAZY);
+ if (!handle) {
+ fprintf(stderr, "%s\n", dlerror());
+ exit(EXIT_FAILURE);
+ }
+
+ dlerror();
+ int (*myfct)() = (int (*)()) dlsym(handle, "myfct");
+ char *error = dlerror();
+ if (error != NULL) {
+ fprintf(stderr, "%s\n", error);
+ exit(EXIT_FAILURE);
+ }
+
+ (*myfct)();
+ dlclose(handle);
+}
diff -Naur missing/runner.c ./runner.c
--- missing/runner.c 1970-01-01 01:00:00.000000000 +0100
+++ ./runner.c 2016-05-15 19:29:27.246102037 +0200
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+int main()
+{
+ void *handle = dlopen(XLIB, RTLD_LAZY);
+ if (!handle) {
+ fprintf(stderr, "%s\n", dlerror());
+ exit(EXIT_FAILURE);
+ }
+
+ dlerror();
+ void (*load_runtimelib)() = (void (*)()) dlsym(handle, "load_runtimelib");
+ char *error = dlerror();
+ if (error != NULL) {
+ fprintf(stderr, "%s\n", error);
+ exit(EXIT_FAILURE);
+ }
+
+ (*load_runtimelib)();
+ dlclose(handle);
+ return 0;
+}
Linux:
(cd runtimelib && gcc -fPIC -Wall runtimelib.c -shared -o libruntimelib.so)
(cd lib && gcc -fPIC -Wall mylib.c -shared -ldl -Wl,-rpath "$(pwd)/../runtimelib" -o libmylib.so)
gcc -fPIC -Wall -DXLIB="\"$(pwd)/lib/libmylib.so\"" runner.c -ldl -o runner
FreeBSD/OSX:
(cd runtimelib && clang -fPIC -Wall runtimelib.c -shared -o libruntimelib.so)
(cd lib && clang -fPIC -Wall mylib.c -shared -Wl,-rpath "$(pwd)/../runtimelib" -o libmylib.so)
clang -fPIC -Wall -DXLIB="\"$(pwd)/lib/libmylib.so\"" runner.c -o runner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment