I haven't read these through (these are not what I based this document on), but these look like really good resources: http://users.actcom.co.il/~choo/lupg/tutorials/libraries/unix-c-libraries.html For more portable use of shared and DL libs, use glib or GNU libtool. USING PROGRAM LIBRARIES (In general, with gcc use "-Wall" to get warnings. Top optimize run "strip" or use gcc option "-Wl,-s" to remove symbols.) Static libs: *.a Shared libs: *.so* To see all the function names in a lib (shared or not), run "nm -o libname.so... | grep ' T '" to list the normal exported symbol definitions. linking ------- You can just give specify a static library file, like "mylib.a". For -lTHING, you need to name the file libTHING.a, and libTHING.a must reside in the library path, which, by default, does not include $PWD: -L. creating static lib ------------------- ar -qv libname NEWFILE Quickly adds NEWFILE to the end. Does no checking. (Note that NEWFILE can be any file of any type). Can "ar -x..." or "ar -t..." just like with tar. NOTE: Author says that you can "dynamically load static libs (.a)", but I find that you can't. shared library files -------------------- Longest name is the "real" name which is libBASENAME.so.IF#.MIN#[.REL#] (This is created with gcc) "soname" links are like libBASENAME.so.IF#. Sym links created by ldconfig. "Linker names" are like libBASENAME.so. Sym links you have to create. creating shared library ----------------------- Create .o files with "gcc -c -fPIC..." (must use the uppercase PIC option). If one of following functions is in lib, they are run when lib is loaded or closed (could be at program exit). Must use "-nostartfiles" to compile. void _init(void); void _fini(void); Create the shared library: gcc -shared -Wl,-soname,REALNAME.so.IF# -o REALNAME.so.IF#.MIN#[.REL#] \ files.o libs... Like gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc (Note that you have to specify any system libs needed by the lib). FOR REVERSE DEPENDENCIES: If your code will call a DL library with unresolved symbols then add compiler switch "-Wl,-export-dynamic". FOR _init OR _fini: Add compiler switch "-nostartfiles". IMPORTANT: the "-soname," option is passed to the executable. It specifies the RUNTIME library file that the executable will look for and use. It does not have to be this file (or a link to this file). The contents of this lib could all be stubs and -soname could be the name of the production "link" that always points to the production library. Now run ldconfig to create the link that you specified in -soname. I think by default ldconfig will do all the directories in /etc/ld.so.conf. To do only one directory, use /sbin/ldconfig -n DIRECTORY ON NEWER SOLARIS thre is no ldconfig. crle -l /usr/lib crle -u -l /dir/to/add using shared libraries ---------------------- When you link your binary you must specify a shared lib to satisfy your symbol dependencies (hereafter called a stub). The "-soname" set inside the library sets the soname looked for and used by your executable (which you can see by running ldd on the executable). When you link your binary, you can specify this stub giving the regular filename (just like a static .a file) or with -lX, but you can only do the latter if the library (or a link to it) ends in .so (i.e., not so.*). Run "ldd PROGNAME" to list the shared libraries used by a program. (Always will list the super-loader ld-linux.so.* and the C library libc.so.*) N.b.: Ignore the listing for linux-gate* that you see on newer Linux kernels. This is a virtual library. Following dirs are searched: $LD_LIBRARY_PATH /lib, /usr/lib list in /etc/ld.so.conf (Can set $LD_PRELOAD for specific functions) DL (Dynamically Loaded) Libraries (Linux- and Solaris-specific). --------------------------------- Caller use #include void * dlopen(char * file, int flag); RETURNS handle or null FLAG must specify when to resolve symbols: RTLD_LAZY or RTLD_NOW NOW is safer but takes all the time up-front at load time. RTLD_GLOBAL may optionally be ORed, meaning to export the imported syms char * dlerror() returns null or the last un-fetched error void * dlsym(void * handle, char *symbol); Returns the value of the named symbol or null (note ambiguity for 0/null values. Must use dlerror() to differentiate.) int dlclose() returns 0 on success. Can do overlaping dlopen's and closes. Deallocated when count is 0. Compile with -ldl (The real-time target lib names are only in the C code). Unlike non-dynamic loading, you don't need to have your shared-library or a stub on-hand at compile time. All you need is libdl.so. Same as for shared libs, _init and _fini are run when libraries are loaded and when ldclose() count reaches 0 (could be when program exits). Special stuff for Kernel modules specifically: http://www.linuxtopia.org/online_books/Linux_Kernel_Module_Programming_Guide/x181.html