Solved

Linux Dynamic Module...

Posted on 2006-06-22
4
205 Views
Last Modified: 2010-04-22
Hi,
Please let me know how to compile this and make this dynamic module work...
I was unable to compile simple-module.c

#include <stdio.h>
#include <stdlib.h>
#ifndef EXIT_FAILURE
#  define EXIT_FAILURE        1
#  define EXIT_SUCCESS        0
#endif

#include <limits.h>
#ifndef PATH_MAX
#  define PATH_MAX 255
#endif

#include <dlfcn.h>
/* This is missing from very old Linux libc. */
#ifndef RTLD_NOW
#  define RTLD_NOW 2
#endif

typedef int entrypoint (const char *argument);

/* Save and return a copy of the dlerror() error  message,
   since the next API call may overwrite the original. */
static char *dlerrordup (char *errormsg);

int
main (int argc, const char *argv[])
{
  const char modulepath[1+ PATH_MAX];
  const char *errormsg = NULL;
  void *module = NULL;
  entrypoint *run = NULL;
  int errors = 0;

  if (argc != 3)
    {
      fprintf (stderr, "USAGE: main MODULENAME ARGUMENT\n");
      exit (EXIT_FAILURE);
    }

  /* Set the module search path. */
  getcwd (modulepath, PATH_MAX);
  strcat (modulepath, "/");
  strcat (modulepath, argv[1]);
 
  /* Load the module. */
  module = dlopen (modulepath, RTLD_NOW);
  if (!module)
    {
      strcat (modulepath, ".so");
      module = dlopen (modulepath, RTLD_NOW);
    }
  if (!module)
    errors = 1;

  /* Find the entry point. */
  if (!errors)
    {
      run = dlsym (module, "run");
      /* In principle, run might legitimately be NULL, so
         I don't use run == NULL as an error indicator. */
      errormsg = dlerrordup (errormsg);

      if (errormsg != NULL)
        errors = dlclose (module);
    }

  /* Call the entry point function. */
  if (!errors)
    {
      int result = (*run) (argv[2]);
      if (result < 0)
        errormsg = strdup ("module entry point execution failed");
      else
        printf ("\t=> %d\n", result);
    }

  /* Unload the module, now that we are done with it. */
  if (!errors)
    errors = dlclose (module);

  if (errors)
    {
      /* Diagnose the encountered error. */
      errormsg = dlerrordup (errormsg);

      if (!errormsg)
        {
          fprintf (stderr, "%s: dlerror() failed.\n", argv[0]);
          return EXIT_FAILURE;
        }
    }
 
  if (errormsg)
    {
      fprintf (stderr, "%s: %s.\n", argv[0], errormsg);
      free (errormsg);
      return EXIT_FAILURE;
    }
 
  return EXIT_SUCCESS;
}

/* Be careful to save a copy of the error message,
   since the next API call may overwrite the original. */
static char *
dlerrordup (char *errormsg)
{
  char *error = (char *) dlerror ();
  if (error && !errormsg)
    errormsg = strdup (error);
  return errormsg;
}



You would compile this on a GNU/Linux machine like so:

 

$ gcc -o simple-loader simple-loader.c -ldl


#include <stdio.h>

int
run (const char *argument)
{
  printf ("Hello, %s!\n", argument);
  return 0;
}



Again, to compile on a GNU/Linux machine:
 

$ gcc -fPIC -c simple-module.c  (This does not work it does not generate .so files)
$ gcc -shared -o simple-module.so (This does not work either)


Having compiled both loader and module, a test run looks like this:
 

$ ./simple-loader simple-module World
Hello, World!
        => 0

 



 

0
Comment
Question by:sudarshantk
4 Comments
 
LVL 43

Accepted Solution

by:
ravenpl earned 63 total points
Comment Utility
my working command:
gcc -c -rdynamic pre_ip.c && ld --shared -o pre_ip.so pre_ip.o -ldl
0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 62 total points
Comment Utility
To build the library, you need to combine your 2 compile lines:
  gcc -fPIC -shared -o simple-module.so simple-module.c
After that, your [program works fine - I tried it. (rather a lot of compiler warnings though)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now