|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
hi, here is my code :
//learn a pointer points to functions #include<stdio.h> #include<stdlib.h> #include<math.h> double Add(double x,double y) {return (x+y);} double Sub(double x,double y) {return x-y;} double Mul(double x,double y) {return x*y;} double Div(double x,double y) {return x/y;} //double Pow(double x,double y) {return pow(x,y);} double (*functable[5])(double,double)={Add,Sub,Mul,Div,pow}; char *msgtable[5]={"Sum","Sub","Mul","Div","Pow"}; int main() { int i; double x=0,y=0; printf("input two numbers:\n"); if(scanf("%lf,%lf",&x,&y)!=2) { printf("wrong input!\n"); exit(1); } for(i=0;i<5;i++) printf("%s:%f\n",msgtable[i],functable[i](x,y)); return 0; } ------------------- when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o: (.data+0x10): undefined reference to `pow' collect2: ld $BJV2s(B 1 what`s wrong, i want to use function of pow,and his header <<math.h>>was include,however, where is the key of the error? have a nice day ![]() |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
erfan wrote:
<irrelevant code snipped> > when i run this programe,gcc gave me an error,that is > :/tmp/cc2ZP8T4.o: (.data+0x10): undefined reference to `pow' > collect2: ld $BJV2s(B 1 > what`s wrong, i want to use function of pow,and his header > <<math.h>>was include,however, > where is the key of the error? > have a nice day ![]() You'd need to link the math lib, done via the "-lm" Compiler/Linker option Bye, Jojo |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
I have successfully compiled your program.
What platform are you on? Some platforms require the -lm option to link the math library. On some (OS X the one I am using now) I guess it is not needed. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
erfan wrote:
> hi, here is my code : [snip] And what has this to do with the subject (which was "Array of pointer and pointer of array")? > when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o: > (.data+0x10): undefined reference to `pow' > collect2: ld $BJV2s(B 1 > what`s wrong, i want to use function of pow,and his header > <<math.h>>was include,however, > where is the key of the error? This looks like FAQ 13.25 - see http://www.c-faq.com |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 1ÔÂ28ÈÕ, ÏÂÎç8ʱ54·Ö, Mark Bluemel <mark_blue...@pobox.com> wrote:
> erfan wrote: > > hi, here is my code : > > [snip] > > And what has this to do with the subject (which was "Array of > pointer and pointer of array")? > > > when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o: > > (.data+0x10): undefined reference to `pow' > > collect2: ld ·µ»Ø 1 > > what`s wrong, i want to use function of pow,and his header > > <<math.h>>was include,however, > > where is the key of the error? > > This looks like FAQ 13.25 - seehttp://www.c-faq.com a-wo, i missed to link the library :-} , thank you all for your tips |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
erfan wrote:
> hi, here is my code : [...] > #include<math.h> [...] > //double Pow(double x,double y) {return pow(x,y);} > double (*functable[5])(double,double)={Add,Sub,Mul,Div,pow}; > char *msgtable[5]={"Sum","Sub","Mul","Div","Pow"}; > int main() > { [...] > exit(1); That's nonportable, use exit(EXIT_FAILURE); [...] > when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o: > (.data+0x10): undefined reference to `pow' collect2: ld 返回 1 > what`s wrong, i want to use function of pow,and his header > <<math.h>>was include,however, Go to http://www.c-faq.com/ and read question 14.3. -- Army1987 (Replace "NOSPAM" with "email") |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
erfan wrote:
[..] > when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o: > (.data+0x10): undefined reference to `pow' > collect2: ld 返回 1 > what`s wrong, i want to use function of pow,and his header > <<math.h>>was include,however, > where is the key of the error? Learn to check the FAQ (as well as your textbook and implementation documentation) before posting. In this case, your question is 13.25 in the FAQ. The FAQ can be found at <http://c-faq.com/>, and it would direct you to <http://c-faq.com/lib/extlibs.html>, the text of which follows. Notice the cross-references at the end. comp.lang.c FAQ list · Question 13.25 Q: I keep getting errors due to library functions being undefined, but I'm #including all the right header files. A: In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the external library itself. The declarations in the header file only tell the compiler how to call the external functions; the header file doesn't supply the definitions of the external functions, or tell the compiler/linker where to find those definitions. In some cases (especially if the functions are nonstandard) obtaining those definitions may require explicitly asking for the correct libraries to be searched when you link the program. (Some systems may be able to arrange that whenever you #include a header, its associated library, if nonstandard, is automatically requested at link time, but such a facility is not widespread.) See also questions 10.11, 11.30, 13.26, 14.3, and 19.40. |
|
![]() |
| Outils de la discussion | |
|
|