From: John Gabriele Date: 2006-09-14T17:09:05+09:00 Subject: Re: building extension modules, and linking On 9/14/06, Vincent Fourmond wrote: > > Hello ! Hi Vincent. Thanks for the reply. > > > 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". Ohhhhhhhhhhhhhhhhhhhh. Ok. Hm. Well then. Maybe that's the point of that "-R" link-editor arg (mentioned in that other post I just made a few minutes ago before seeing this one). The ld docs on -R/-rpath don't specifically say what you so clearly express above, but they might *imply* that if read under the right intensity lights, wearing those cheap bi-colored 3D-movie glasses, while howling under a full-moon... :) > 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. Got it. > 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 Thanks again Vince! :) ---John