Ah. Nodeshell planted by the guy that noded function pointers.

libdl is a programming interface to the dynamic linker for user applications on UNIX-like operating systems, originating from Solaris. It gives programs the ability to call abitrary functions in arbitrary libraries at runtime, with library and symbol names based on strings. It can be used for lots of things, notably plug-ins and the like.

It's incredibly simple to use. First, there are library handles, which are void pointers. To get a library handle, you call dlopen(), which takes two args: library name string and an open mode. Once you open a library (assuming it's there), you can get function pointers and the like with dlsym(library_handle, symbol_name_string).

Let me do a little "Hello World" with dynamic linking... Consult your manpages for dlsym, dlopen, dlclose, etc. if interested.
/* dlhello.c: hello world with dymanic linking.
 * compile with -ldl
 */

#include <dlfcn.h>
#include <stdlib.h>		/* for NULL, EXIT_FAILURE, EXIT_SUCCESS... */

#define LIBC	"libc.so.6"

int main() {

	void *libc;	/* handle to the libc we are going to open */
	int (*printf)(const char*,...);

	libc = dlopen( LIBC, RTLD_LAZY );
		/* RTLD_LAZY has to do with the way symbols are resolved.
		 * read up on your dlopen() manpage for more info. */
	
	if( libc == NULL ) {
		/* dlerror() for error string.  but we have no (f)printf, so
		 * let's just exit quietly. */
		return EXIT_FAILURE;
	}

	printf = dlsym( libc, "printf" );
	if( printf == NULL ) {
		dlclose( libc );
		return EXIT_FAILURE;
	}
	
	printf( "Hello, dynamically linked world!\n" );
	dlclose( libc );
	return EXIT_SUCCESS;
	
}

Log in or register to write something here or to contact authors.