# dlopen help

**URL:** https://community.unix.com/t/dlopen-help/212846
**Category:** Programming
**Created:** [September 17, 2008, 10:13am UTC](https://community.unix.com/t/dlopen-help/212846 "2008-09-17T10:13:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lookforlohith](https://community.unix.com/letter_avatar/lookforlohith/32/5_5575768a8748004e209b776fc1b2916d.png) [@lookforlohith](https://community.unix.com/u/lookforlohith)
#### Post date: [September 17, 2008, 10:13am UTC](https://community.unix.com/t/dlopen-help/212846/1 "2008-09-17T10:13:26Z")

</div>

```nohighlight
//foo.c

#include<stdio.h>

int pen(int a)
{
    printf("%d",a);
}

$cc -c foo.c
$ls -shared -o libfoo.so foo.o

```

```nohighlight
///////////now libfoo.so formed 
//i have already designed libfoo.so
//main.c

#include<stdio.h>
#include <dlfcn.h>
int main()
{
   int (*p)(int)=NULL ;
   void *handle;
   handle=dlopen("libfoo.so",RTLD_LAZY);
   p=dlsym(handle,"pen");
   (*p)(5);
   return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

```

but this gives error

```nohighlight
$ cc main.c -ldl
$ ./a.out
Segmentation fault

```

---

<div class="post-metadata">

### Author: ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)
#### Post date: [September 19, 2008, 7:33am UTC](https://community.unix.com/t/dlopen-help/212846/2 "2008-09-19T07:33:18Z")

</div>

You have no error checking, for starters. This will tell you where things go wrong.

```nohighlight
#include<stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
int main()
{
   int (*p)(int)=NULL ;
   void *handle;
   handle=dlopen("libfoo.so",RTLD_LAZY);
   if (!handle) 
  {
	fputs (dlerror(), stderr);
	exit(1);
   }

   p=dlsym(handle,"pen");
   {
          char *err=dlerror();
          if(err!=NULL)
          {
                fputs(err,stderr);
                exit(1);                
           }
    }

   (*p)(5);
   return 0;
}

```

---

<div class="post-metadata">

### Author: ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/5_5575768a8748004e209b776fc1b2916d.png) [@shamrock](https://community.unix.com/u/shamrock)
#### Post date: [September 19, 2008, 1:27pm UTC](https://community.unix.com/t/dlopen-help/212846/3 "2008-09-19T13:27:00Z")

</div>

In addition to error checking you need to cast the operands and function return values to the proper type before assignment. Source code changes highlighted in red.

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

int main(void)
{
   int (*p)(int)=NULL;
   int i;
   void *handle;
   handle=dlopen("libfoo.so",RTLD_LAZY);

   /* cast the return value from dlsym() to the proper type before assignment to p
      p is a pointer to a function that takes an int argument and return an int */
   p=((int) (*)(int)) dlsym(handle,"pen");
   
   /* capture the integer return value from the shared library */
   i = (*p)(5); 
   return 0;
}

```
