?
Solved

calling function from .so file, dlsym() problem

Posted on 2006-03-28
15
Medium Priority
?
239 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.
0
Comment
Question by:trubiat
  • 4
  • 4
  • 3
11 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 16313803
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
 

Author Comment

by:trubiat
ID: 16313838
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
 

Author Comment

by:trubiat
ID: 16313845
i have #include <dlfcn.h> at the top so i don't know what that error means.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:Axter
ID: 16313876
Check your man page for dlopen, to make sure you have the right #include
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 16313889
You'l need to link with libdl.a also, e.g. like

gcc -rdynamic -o foo foo.c -ldl
0
 

Author Comment

by:trubiat
ID: 16313891
that';s the right one...Any other ideas?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16313914
Yes, use the library I mentioned in my last comment ;o)

This is a linker error, not a compiler error.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16313921
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 16313955
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
 
LVL 86

Expert Comment

by:jkr
ID: 16313963
Agree, 100%. That should have been a split.
0
 

Author Comment

by:trubiat
ID: 16314008
sorry guys my fault...how do i give points to both of you?

please administrator allow me to award the points again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question