Results 1 to 5 of 5
I'm writing a program that uses dynamic library to invoke the double pow(double,double) function in libm.so ( runtime linking ,which uses dlopen(),dlsym()....) and also defines a function pow of my ...
- 03-22-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Dynamic library function - type conflicts
I'm writing a program that uses dynamic library to invoke the double pow(double,double) function in libm.so ( runtime linking ,which uses dlopen(),dlsym()....) and also defines a function pow of my own . My own func is int pow(int,int) . You may say that it's not necessary , but the thing is I just want to explore , and , moreover , we can pick any other functions - the pow is just what I'm asked to do...(It's an assignment !)
The plan is use pointer to function and assign the addr of the pows and then run them . The program runs well with both invocation of the dynamic lib version and my own version . But there is a warning "conflict types with built-in pow" when I compile :
How can I get rid of the warning ? And I would appreciate an explanation about dynamic library functions , how they are used in the running of the program , and symbol resolution (to avoid things like this) .Code:gcc -rdynamic -o main main.c -ldl
Regards .
- 03-22-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
What you are trying to do is function overloading, and it's a no-go on C. Standard C doesn't allow function overloading nor polymosfism, and you are going to have serious problems if you start using things like this.
There are some ways around it, and there are even compilers that can handle this, but as I said, this is not a good thing to do. If you want to use overloading, use C++ instead. On C++ you can have many functions with the same name, but different data types (even with different number of arguments). The compiler will pick the correct one depending on the arguments, or, if no matching function can be found, it will usually show an errors telling you what the closest candidates are or even try to make some automatic casts to adapt the arguments to the closer data types that matches an overloaded function.
What you are trying to do here, definitely, is not C.
You could always however write a wrapper function, but I highly doubt that your teachers want to see that, because they will inmediatly know that you copied it from somewhere else. :P
- 03-24-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
But is there anyway to get rid of the warnings here ?
- 03-24-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
If your code is writen the way I think it is, then most compiler will not even compile it. And those who do, will print warnings. Each compiler has a way to disable warnings of various types if you don't want to see them.
For gcc, look at the man page, for the various -W<foo> options. I have never tried this in C, since I know it's incorrect. So, I can't help you and I am not willing to dive though the gcc man page to find about this.
- 03-24-2008 #5
So here's what I bet is happening: this doesn't actually have anything to do with the dynamic part of this. This has to do with you having written a function that matches a standard function, but has different types. For instance, I have this file:
When I compile:Code:double pow(int a, int b) { return a * b; } int main() { return 0; }
gcc believes that you are implementing your own version of the standard pow() function for whatever reason, and it is warning you that you are doing it imperfectly.Code:alex@danu ~/test/c $ gcc -Wall wrong_types.c wrong_types.c:2: warning: conflicting types for built-in function ‘pow’
Does this help?DISTRO=Arch
Registered Linux User #388732


Reply With Quote
