Nytro Posted October 17, 2009 Report Share Posted October 17, 2009 Loading and unloading shared libraries can be tricky sometimes. The biggest problem is when you use the g++ compiler (as opposed to the gcc compiler) it mangles the symbols in the object file so you can't load them later. Here's a "Hello World!" dynamic library in C++ example for your enjoyment.First, create the library file, called dynamic.cp#include <cstdio>extern "C" void _init();extern "C" void hello(void);extern "C" void _fini();void _init() { printf("Initialization Phase");}void hello(void) { printf("Hello, Dynamic World!");}void _fini() { printf("Deconstruction Phase");}The _init() and _fini() functions will be called automagically when you load or unload the dynamic module.You can compile this on the command line (we'll create a Makefile in a minute):g++ -fPIC -c dynamic.cppld -shared -o dynamic.so dynamic.oWe first compile the function with g++ first because if we just do the whole thing together it automatically adds tons of other symbols you just don't need, not to mention not allowing you to put your own _init() and _fini() functions in. If you're curious, go ahead and try to compile it with the command g++ -fPIC -shared -o dynamic.so dynamic.cpp—it probably won't work!Next, we need a program to test it with, and in a fit of creative genius I called it main.cpp:#include <cstdio>#include <cstdlib>#include <dlfcn.h>int main() { void *handle; void (*hello)(void); char *error; handle = dlopen("/path/to/my/dynamic.so", RTLD_LAZY); if(handle == NULL) { fprintf(stderr, "Error: open/load error of dynamic.so failed: %s\n", dlerror()); exit(1); } hello = (void(*)(void)) dlsym(handle, "hello"); if((error = dlerror()) != NULL) { fprintf(stderr, "Error: symbol lookup in dynamic.so failed: %s\n", dlerror()); exit(2); } hello(); dlclose(handle); return 0;}Now all we need is a good Makefile:# Makefile for dynamic module loadingAPP = testCC = g++LD = ldall: $(APP)dynamic.o: dynamic.cpp $(CC) -fPIC -c dynamic.cppdynamic.so: dynamic.o $(LD) -shared -o dynamic.so dynamic.o$(APP): dynamic.so main.cpp $(CC) -o $(APP) main.cpp -ldlclean: @rm -f *.o *.so $(APP)I realize I don't explain a lot of this code, but it's not too complicated, so it should be pretty easy to understand if you are familiar with C or C++.Run-Time Linking of Shared LibrariesIt's been some time since I updated this page, but I thought I'd mention the run-time linking of shared libraries option. If you create a shared library as above, and install it somewhere in your /etc/ld.so.conf path, you can compile and link to the library at run-time. This means you don't have to bother with the dlopenand dlclose functions, but the shared library must be available at compile time.First, create your library a little differently:ld -shared -o libdynamic.so.1.0 dynamic.oThen copy it to a nice place like /usr/local/lib and create some symlinks:cp libdynamic.so.1.0 /usr/local/libln -s /usr/local/lib/libdynamic.so.1.0 /usr/local/lib/libdynamic.so.1ln -s /usr/local/lib/libdynamic.so.1.0 /usr/local/lib/libdynamic.soThe first ln command makes this whole business work with the run-time linker, the second makes the syntax -ldynamic work. Make sure you run ldconfig as root after you install a new library!Now when you compile, just add the -ldynamic to your link command and you'll be ready to go!Autor: Scurvy JakeVa recomand sa cititi acest tutorial ( cei care veti scrie programe pentru Linux ) Quote Link to comment Share on other sites More sharing options...