From: Vincent Fourmond Date: 2006-09-14T16:25:20+09:00 Subject: Re: building extension modules, and linking Hello ! > But when we're building an extension module, how to you do tell GCC > (at link-edit time) that you want your code to link to (at runtime) > what's already loaded and running -- that is, to link to the ruby > interpreter -- rather than to some shared lib somewhere? Well, this is highly platform-dependant. If I understand you right, you're asking how the system knows where to find the rb_* functions called from within your C code, even if you didn't link with the appropriate library ? The answer is relatively simple. The Makefile produced by extconf.rb contain linker-specific flags to say : "don't bother to look for missing symbols at link-time, do this at run-time". What happens next is when your extension is loaded, the dynamic linker see there are missing symbols, and it looks for them in the current namespace. From within a ruby interpreter, the symbols are already here and everything goes fine. But if for some reasons it doesn't find symbols, your program will crash with an undefined symbol problem. Just as an example try, with a ruby extension int main() { void * a; void (* func)(); a = dlopen("ruby_extension.so"); func = dlsym(a, "Init_extension"); /* might already crash here, make sure to use the right Init_ function */ func(); /* will crash here if not before */ } If you want to inspect this in more details, I advise you to have a look at nm and especially the output of nm -D on a shared object (small, if possible). Cheers ! Vince