[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: plugins

> how can I load shared libraries by my programm?
> How does that work genrerelly?

	After reading the man pages for dlopen, close, etc, I threw together a
quick program to make sure I knew what I was doing, and it seems to work:


#include <stdio.h>
#include <dlfcn.h>

main() {
   char dlname[]="./dynamic1.so";
   int i;
   char *error;
   void *handle;
   void (*func)(void);

   printf("Which library should I load? (1, 2) : ");
   scanf("%d", &i);
   dlname[9] = i + '0';

   handle = dlopen(dlname, RTLD_LAZY);
   if(!handle) {
      printf("Error: %s\n", dlerror());
   func = dlsym(handle, "function");
   if ((error = dlerror()) != NULL)  {
      printf("Error: %s\n", error);

} /* End */


#include <stdio.h>

void function( void ) {
   printf("This is from dynamic1.so\n");


#include <stdio.h>

void function( void ) {
   printf("This is from dynamic2.so\n");


	Just compile the three files like so:

gcc -rdynamic -ldl -o dltest dltest.c
gcc -shared -o dynamic1.so dynamic1.c
gcc -shared -o dynamic2.so dynamic2.c

	And run dltest. It's obviously a VERY simple example, but it works,
and it even does some error checking, and I threw it together in about 2
minutes, so it shows how easy this stuff actually is. (Since I DID throw it
together in 2 min, can anyone see any problems I may have missed?)