We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

calling function from .so file, dlsym() problem

trubiat
trubiat asked
on
Medium Priority
310 Views
Last Modified: 2010-04-15
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.
Comment
Watch Question

Senior Software Engineer
Commented:
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!

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Author

Commented:
i have #include <dlfcn.h> at the top so i don't know what that error means.
AxterSenior Software Engineer

Commented:
Check your man page for dlopen, to make sure you have the right #include
jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
You'l need to link with libdl.a also, e.g. like

gcc -rdynamic -o foo foo.c -ldl

Author

Commented:
that';s the right one...Any other ideas?
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Yes, use the library I mentioned in my last comment ;o)

This is a linker error, not a compiler error.
AxterSenior Software Engineer

Commented:
>>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.
AxterSenior Software Engineer

Commented:
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?
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Agree, 100%. That should have been a split.

Author

Commented:
sorry guys my fault...how do i give points to both of you?

please administrator allow me to award the points again
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.