• C

calling function from .so file, dlsym() problem

I am having trouble understanding the following error message:

dlexec.c: In function `main':
dlexec.c:44: void value not ignored as it ought to be

I have an add.so library which has and add function in it that takes two Strings as arguments and returns an int. I want to call this function from my main() program. The "add" name and the 2 numbers are passed as arguments in my main() function.

Here is my code:

int main(int argc, char *argv[])
{
      if (argc - 1 != 3)
      {
            printf("Wrong number of parameters\n");
      }
      else
      {
            char name[100] = "./";
            char app[4] = ".so";
            void *handle;
            void (*func)(char*, char*);
            int result;
            //int result = add(argv[2], argv[3]);

            strcat(name, argv[1]);
            strcat(name, app);
            printf("%s\n", name);
            handle = dlopen (name, RTLD_LAZY);
            if (!handle)
            {
                  fputs (dlerror(), stderr);
                                                exit(1);
                                }
            func = dlsym(handle, argv[1]);
            result = (int)func(argv[2], argv[3]);
            printf("RESULT = %d\n", result);


            printf("%s\n", argv[2]);
            printf("%s\n", argv[3]);
      }
}


Thanks for your help.
trubiatAsked:
Who is Participating?
 
AxterCommented:
Hi trubiat,
> void (*func)(char*, char*);
Your function declartion is void.

If your function return an int, then you need a function declaration that returns an int
int (*func)(char*, char*);

David Maisonave (Axter)
Cheers!
0
 
trubiatAuthor Commented:
by doing that i get the following errors:

/tmp/cc52OCAo.o: In function `main':
dlexec.c:(.text+0x97): undefined reference to `dlopen'
dlexec.c:(.text+0xb6): undefined reference to `dlerror'
dlexec.c:(.text+0xe3): undefined reference to `dlsym'
collect2: ld returned 1 exit status

thanks
0
 
trubiatAuthor Commented:
i have #include <dlfcn.h> at the top so i don't know what that error means.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
AxterCommented:
Check your man page for dlopen, to make sure you have the right #include
0
 
jkrCommented:
You'l need to link with libdl.a also, e.g. like

gcc -rdynamic -o foo foo.c -ldl
0
 
trubiatAuthor Commented:
that';s the right one...Any other ideas?
0
 
jkrCommented:
Yes, use the library I mentioned in my last comment ;o)

This is a linker error, not a compiler error.
0
 
AxterCommented:
>>result = (int)func(argv[2], argv[3]);


Now that you fixed the func declaration, you can remove the above (int) cast.

When ever you find yourself needing to do a cast, double check your code, because that's the compiler's way of saying there's some logic error in the code.
Adding a cast to it, just silences the compiler, but doesn't make the problem go away.
0
 
AxterCommented:
trubiat,
I answered your original question, which allowed you to successfully compile the code.

Is there a reason why I wasn't awarded points for answering the question?
0
 
jkrCommented:
Agree, 100%. That should have been a split.
0
 
trubiatAuthor Commented:
sorry guys my fault...how do i give points to both of you?

please administrator allow me to award the points again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.