C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 12:07 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 5
Question : undefined reference to `pow'

Hi all,

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   Reply With Quote
Old 02-09-2010, 12:28 AM   #2
Woof, woof!
 
zacs7's Avatar
 
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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 02-09-2010, 12:44 AM   #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   Reply With Quote
Old 02-09-2010, 01:02 AM   #4
&TH of undefined behavior
 
Fordy's Avatar
 
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   Reply With Quote
Old 02-09-2010, 01:17 AM   #5
Registered User
 
Join Date: Feb 2010
Posts: 5
Sorry that was an accident
mohitsaxena019 is offline   Reply With Quote
Old 02-09-2010, 04:17 AM   #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)
{
   
}
oh and the second time use:

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   Reply With Quote
Old 02-09-2010, 04:21 AM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,320
Quote:
Originally Posted by kille6525
ok if your going to use a function you have to declare it before the main loop.
That is pretty much what zacs7 already stated, except that it was pointed out that you might not have to declare a prototype, though the results may then not be what you expect.

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   Reply With Quote
Old 02-09-2010, 07:48 AM   #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   Reply With Quote
Old 02-09-2010, 07:50 AM   #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   Reply With Quote
Old 02-09-2010, 07:59 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,320
Quote:
Originally Posted by mohitsaxena019
Sorry laserlight i am agree with you but this is not the question which I am facing Plz read it clearly
zacs7 has already answered your question: you need to forward declare pow, otherwise the signature and return type assumed will not match the pow from <math.h>. Of course, the simplest solution is to just #include <math.h> as you should have done in the first place.

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   Reply With Quote
Old 02-09-2010, 08:04 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22