// foo.c // represents the XS lib referencing a symbol (lgamma) in an external library libm.so #include double foo() { return lgamma(42.0); } // mytest.c // this is just a wrapper which essentially does what perl/DynaLoader is doing, // i.e. loading the XS lib (Foo.so) dynamically at runtime #include #include #include int main() { double (*foo)(void); char *error; printf("dynamically loading 'Foo.so'...\n"); void *so = dlopen("Foo.so", RTLD_LAZY); if (!so) { fprintf(stderr, "%s\n", dlerror()); exit(1); } dlerror(); *(void **) (&foo) = dlsym(so, "foo"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); exit(1); } printf("%f\n", (*foo)()); dlclose(so); exit(0); } #### gcc -c -fPIC foo.c # compile gcc -shared -o Foo.so foo.o # create/link shared library Foo.so gcc -rdynamic -o mytest mytest.c -ldl # create wrapper executable which loads Foo.so #### $ LD_LIBRARY_PATH=. ./mytest dynamically loading 'Foo.so'... ./mytest: symbol lookup error: ./Foo.so: undefined symbol: lgamma #### $ gcc -shared -o Foo.so foo.o -lm $ LD_LIBRARY_PATH=. ./mytest dynamically loading 'Foo.so'... 114.034212