• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
trubiatAuthor Commented:
i have #include <dlfcn.h> at the top so i don't know what that error means.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

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

This is a linker error, not a compiler error.
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.
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?
jkrCommented:
Agree, 100%. That should have been a split.
trubiatAuthor Commented:
sorry guys my fault...how do i give points to both of you?

please administrator allow me to award the points again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.