1

I made a simple memory manger and I'm trying to compile it using shared library in the driver.

The shared library files itself compiles fine. However, when I compiled the driver by calling the functions in the shared library with my memory manager, it shows me the screenshot below:error messages

Here are my code for the shared.c

/* conveniences for casting and declarations */
typedef block_info* (*MM_CREATE)(size_t, MMPolicy);
typedef void* (*MM_ALLOCATE)(block_info *, size_t, char *);
typedef int (*MM_DEALLOCATE)(block_info *, void *);
typedef void (*MM_DESTROY)(block_info *);

/* Function pointers retrieved from the shared library */
typedef struct LibraryFunctions
{
   MM_CREATE create;
   MM_DESTROY destroy;
   MM_ALLOCATE allocate;
   MM_DEALLOCATE deallocate;
}LibraryFunctions;

/* Loads a shared library and returns a pointer to it in libhandle */

/* Returns SUCCESS, if it successful, otherwise, FAILURE           */
int load_library(const char *libname, void **libhandle)
{

*libhandle = dlopen(*libhandle, RTLD_LAZY);

if(!(*libhandle))
{
    return FAILURE;
}
else
{
    return SUCCESS;
}

return *libname;
}
int get_functions(void *libhandle, 
               LibraryFunctions *functions, 
               const char **fn_names)
{
functions->create = (MM_CREATE)(intptr_t)dlsym(libhandle, *fn_names);

if(!functions->create)
{
    return FAILURE;
}

functions->destroy = (MM_DESTROY)(intptr_t)dlsym(libhandle, *fn_names);

if(!functions->destroy)
{
    return FAILURE;
}

functions->allocate = (MM_ALLOCATE)(intptr_t)dlsym(libhandle, *fn_names);

if(!functions->allocate)
{
    return FAILURE;
}

functions->deallocate = (MM_DEALLOCATE)(intptr_t)dlsym(libhandle, *fn_names);

if(!functions->deallocate)
{
    return FAILURE;
}

return SUCCESS;

} Here is part of the driver code to call the shared library:

void setup(void)
{
  const char *fn_names[] = {"mm_create", "mm_destroy", "mm_allocate",   "mm_deallocate"};
  LibraryFunctions funs;
  int error;

  error = load_library("./libmemmgr.so", &gLib);
  if (error == FAILURE)
  {
    printf("load_library failed! %s\n", dlerror());
    exit(-1);
  }

  error = get_functions(gLib, &funs, fn_names);
  if (error == FAILURE)
  {
    printf("get_functions failed! %s\n", dlerror());
    exit(-1);
  }

  mmlib_create = funs.create;
  mmlib_destroy = funs.destroy;
  mmlib_allocate = funs.allocate;
  mmlib_deallocate = funs.deallocate;

}

void teardown(void)
{
  dlclose(gLib);
}

I'm not sure what is causing the errors.

edit: So I managed to run the exe file but I failed on "get_functions". Are there any problems with my get_functions?

1
  • 1
    add ` -ldl` to your compilation if not already included
    – j-p
    Jul 9, 2015 at 22:44

1 Answer 1

3

You don't seem to be linking against the dl library, where dlclose and dlerror are defined (at least on Linux).

You're also not linking against your own shared library, where you defined the function load_library. You don't show the code that defines get_functions at all, so I'm not sure where that lives.

So: your shared library should be named libfoo.so, and you need to add -L. -lfoo -ldl to your compiler command line, after all the .c files. The -L. will tell the compiler to add the current directory to the library search path, the -lfoo will tell it to link against the libfoo.so from the current directory, and the -ldl will tell it to also link against a libdl.so somewhere else in the library search path (probably /usr/lib).

2
  • I updated the code that include the "get_functions" function.
    – TommyLan
    Jul 9, 2015 at 22:56
  • I followed your instructions and added -L. -lfoo -ldl. Now the dl errors are gone. But I still can't access those two functions.@godlygeek
    – TommyLan
    Jul 9, 2015 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .