dlopen, dlsym does not work in Linux as expected

This is a discussion on dlopen, dlsym does not work in Linux as expected within the C Programming forums, part of the General Programming Boards category; The following is the code where the API dlopen, dlsysm works different in Linux and Solaris, File: dltrylib.c **************** Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    2

    dlopen, dlsym does not work in Linux as expected

    The following is the code where the API dlopen, dlsysm works different in Linux and Solaris,

    File: dltrylib.c
    ****************
    Code:
    #include<stdio.h>
    extern void printdltry();
    
    void dltrylib_initialise()
    {
    printf("I am in library\n");
    printdltry();
    }
    File:dltry.c
    ***********
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <dlfcn.h>
    #include <fcntl.h>
    void printdltry();
    
    main()
    {
    
    void (*userfct_init)(); /* User initialize function returned by load()*/
    char *libdir = NULL; /*Directory to search the .so*/
    void *handle; /* Handle on the dynamic linked library*/
    char pathso[100];
    char userfct_name[256];
    
    handle = dlopen( "/tmp/dltry/dltrylib.so", RTLD_NOW );
    
    if ( NULL != handle )
    {
    memset( userfct_name, '\0', sizeof(userfct_name) );
    (void)strcpy( userfct_name, "dltrylib_initialise");
    
    userfct_init = (void (*)())dlsym( handle, userfct_name );
    
    if ( NULL != userfct_init )
    {
    /* ... Load complete. Call the initialize function ...*/
    (void)(*userfct_init)();
    }
    else
    {
    fprintf(stderr, ": %s\n", dlerror());
    }
    
    (void)dlclose( handle );
    printf("Main: Library Loaded successfully loaded\n");
    
    else
    {
    printf("Unable to load the library\n");
    fprintf(stderr, ": %s\n", dlerror());
    
    }
    
    }
    
    void printdltry()
    {
    printf("dlxxxx services work perfectly fine\n");
    }
    Compilation steps followed:
    ***************************
    a) cc -g -c dltrylib.c
    b) ld -G -o /tmp/dltry/dltrylib.so dltrylib.o
    c) cc -g -c dltry.c
    d) cc -o dltry dltry.o -ldl -lrt

    From Solaris machine:
    *********************
    1. Above Compilation procedure,
    2. Run,
    >dltry
    Lib: I am in shared library
    dlxxxx services work perfectly fine
    Main: Library loaded successfully loaded

    From Linux machine:
    *******************
    1. Above Compilation procedure followed,
    2. Run,
    >dltry
    Unable to load the library
    : /tmp/dltry/dltrylib.so: undefined symbol: printdltry

    3. Now, if the step d) in compilation is changed as
    > cc -o dltry dltry.o /tmp/dltry/dltrylib.so -ldl -lrt
    4. Now, run
    >dltry
    Lib: I am in shared library
    dlxxxx services work perfectly fine
    Main: Library loaded successfully loaded

    Can somebody tell me why this shared library (dltrylib.so) needs to be linked in linux? or What is the alternative solution for this?
    This means the dl routines in linux does not suport the direct access to the dynamic linking facilities as expected.



    Any support on this line is highly appreciated.
    Thanks for your understanding.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Please fix your indentation. With your nested code blocks it's very difficult to see where stuff lies.

    Try linking dltry (the program, not the library) with -rdynamic.

    From man dlopen:
    External references in the library are resolved using the libraries in that library's dependency list and any other libraries previously opened with the RTLD_GLOBAL flag. If the executable was linked with the flag "-rdynamic" (or, synonymously, "--export-dynamic"), then the global symbols in the executable will also be used to resolve references in a dynamically loaded library.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    2
    Thanks for your valuable tips.

    I studied the manual, and found the option "-rdynamic" (or, synonymously, "--export-dynamic") which should have solved my problem.
    But when I tried with "cc --export-dynamic -o dltry dltry.o -ldl -lrt", it was unsuccessfull.

    Also,"cc --help -v" gave only
    "-E, --export-dynamic Export all dynamic symbols",
    as it does not support "-rdynamic" option.

    Do let me know how to solve my problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Why this do-while loop doesn't work as I expected?
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-25-2002, 09:47 AM
  3. Linux/Xp
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-17-2002, 11:34 AM
  4. Linux Under Windows
    By Strut in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2002, 08:09 PM
  5. linux vs linux?
    By Dreamerv3 in forum Linux Programming
    Replies: 5
    Last Post: 01-22-2002, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21