![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 5
| I am playing with pow() function using gcc compiler. I have got some intresting error while playing. Here I am writing the code as well as the problem main() { int i,j,k; j=10; k=2; i=pow(j,k); printf("i=%d",i); } It is the Complete code no math directory included. when i compile this using the following command: gcc test.c It gives the following error /var/tmp//ccUP0AVk.o(.text+0x3f): In function `main': : undefined reference to `pow' when i compile this using the following command: gcc -lm test.c It gives no error and correct output Now I had make some changes in the program main() { int i,j,k; j=10; k=2; i=pow(10,2); printf("i=%d",i); } when i compile this using the following command: gcc test.c It gives no error and correct output Q1:-) Is it not necessary to include files in the code. Q2:-) Why it is not giving error in the second code. Plz help me out. |
| mohitsaxena019 is offline | |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,295
| Q1. Because of the default prototype presumed by the compiler for "pow()". It's a bad idea to not include the prototype. See the C89 standard for more information. Q2. Probably left over object files and linkage. Delete all the generated files (including ones in tmp) then you'll most likely get the error again. You might want to use the code tags next time you post code. See << !! Posting Code? Read this First !! >> You should be using prototypes, and you shouldn't be declaring main implicitly. It returns int and takes either void arguments, or int, char **. So declare it as such. |
| zacs7 is offline | |
| | #3 |
| Registered User Join Date: Feb 2010
Posts: 5
| Sorry i did'nt think that these are the answers of my questions |
| mohitsaxena019 is offline | |
| | #4 |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,218
| mohitsaxena019 - why did you report this to the moderators? It wont get you any help and if you do it again I'll close the thread
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules |
| Fordy is offline | |
| | #5 |
| Registered User Join Date: Feb 2010
Posts: 5
| Sorry that was an accident |
| mohitsaxena019 is offline | |
| | #6 |
| Registered User Join Date: Feb 2010
Posts: 6
| ok if your going to use a function you have to declare it before the main loop. Code: #include<stdio.h>
/*declars the function*/
int pow(int j, int k);
int main(void)
{
return 0;
}
int pow(int j, int k)
{
}
gcc -Wall -Werror -ansi -pedantic main.c -o main.exe that will probly give you the error look up my vids on youtube for more info about functions |
| kille6525 is offline | |
| | #7 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,320
| Quote:
Anyway, I do not suggest naming a function pow in view of a possible conflict with pow from <math.h>, which mohitsaxena019 apparently is using, considering the -lm option.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #8 |
| Registered User Join Date: Feb 2010
Posts: 5
| Dear May be both of you do'nt know that pow() is a inbuilt function. It is their in math.h library |
| mohitsaxena019 is offline | |
| | #9 |
| Registered User Join Date: Feb 2010
Posts: 5
| Sorry laserlight i am agree with you but this is not the question which I am facing Plz read it clearly |
| mohitsaxena019 is offline | |
| | #10 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,320
| Quote:
EDIT: Okay, I see that I did misread your question. You should have stated why you think zacs7's answers are not satisfactory. Logically, the effects of default int do not seem to be in effect here, which is a little puzzling, but is confirmed by you being able to compile without including the header. Consequently, I suspect that you may be observing something implementation defined: the answer is just don't do it.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 02-09-2010 at 08:07 AM. | |
| laserlight is offline | |
| | #11 |
| Super Moderator Join Date: Sep 2001
Posts: 4,746
| You provided constants for both the inputs to pow, so I'm betting the compiler made an optimization and replaced the call with the literal value, or inlined pow for you, since pow(10,2) will always be the same value, and you didn't define pow in any way. That seems to be the case with this thread: gcc and pow in a C program - JustLinux Forums, where they actually look at the assembly outputted by gcc to see what was going on. edit: as laserlight implies, this is NOT part of the C standard in anyway - it's specific to gcc, and even then, perhaps to only certain versions - so don't use it in your code or rely on it. |
| sean is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| Undefined Reference Compiling Error | AlakaAlaki | C++ Programming | 1 | 06-27-2008 11:45 AM |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| C OpenGL Compiler Error? | Matt3000 | C Programming | 12 | 07-07-2006 04:42 PM |
| c++ linking problem for x11 | kron | Linux Programming | 1 | 11-19-2004 10:18 AM |