dlopen success, dlsym returns undefined symbol

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    dlopen success, dlsym returns undefined symbol

    opening shared lib with dlopen, gets success, but dlsym returns an error saying undefined symbol, also passing "-rdynamic" during linking this shared lib, any idea?. dlopen success, dlsym gets error, also when I do nm on this shared lib, I can see symbol, this is standalone lib, not linked with executable.


    if ((handle = dlopen(libname, RTLD_NOW)) == NULL) {
    printf("ERROR: cannot load library: %s, %s\n", libname, dlerror());
    exit(1) ;
    } else {
    ret = dlsym(handle, func_name);
    if ((error = dlerror()) != NULL) {
    printf("ERROR: load condition function(%s) failed in %s: error:%s\n",
    func_name, libname, error );
    exit(1);
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Is this C or C++?

    C++ requires func_name be extern "C" in the shared library.

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      This is C code only, but file names are .cpp. eventhough C code, do I need "extern "C"?.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If the file names are .cpp I expect you have a C++ compiler.

        If so, you will need the extern "C" when you build the shared library OR you must change the project settings to compile the .cpp files as C code only.

        I say this since most C compiles use .c for this implementation files.

        Comment

        • tvnaidu
          Contributor
          • Oct 2009
          • 365

          #5
          Thanks, it works, now dlsym works.

          but when I tried to execute the function which was in shared lib, it fails. this func1 is part of shared lib was opened above using dlopen, then dlsym gets success, execution fails?. any idea?.


          (func1)(param1, param2, param3).

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            dlsym returns a void*. This is the address of the function in the library.

            If this library function looks like:

            Code:
            void MyFunc(int a, int b);
            Then you must typecast the void* returned from dlsym into a pointer to a function that takes two int arguments and returns a void.

            Then you use the typecast pointer to make the call:

            ret = dlsym(handle, func_name);

            void (*MyPointer)(in t, int) = (void (*)(int,int))re t;

            MyPointer(3,4);

            Comment

            • tvnaidu
              Contributor
              • Oct 2009
              • 365

              #7
              Thanks for your posting.

              Comment

              Working...