# Thread: pow(x, y); problems

1. ## pow(x, y); problems

This program is supposed to output the Power of 2's Table:
Code:
```#include <stdio.h>
#include <math.h>

int main()
{
double two;
int exponenet;

puts("Power of 2's Table");
for(exponent=0; exponent <= 10; exponent++)
{
two = pow(2.0, (double)exponent++);
printf("2 to the %2d power is %.0f\n", exponent, two);
}
return(0);
}```
and this is the error i recive:

Code:
```/tmp/ccHyVJOk.o: In function `main':
twostable.c:(.text+0x3b): undefined reference to `pow'
collect2: ld returned 1 exit status```
Any input on why this is not working will be greatly appreciated.. thank you.

2. with some compilers you have to explicitly link the math.h library. e.g: gcc -o main.exe main.c -lm

3. Another example;
Code:
```/* Goal of this problem is to find out if sqrt of 5 is the same as 5^1/2*/

#include <stdio.h>
#include <math.h>

int main()
{
double five;
double onehalf;

puts("Is the square root of 5 equal to 5 to 1/2 power.. lets find out!");

five = sqrt(5.0);
onehalf = pow(5.0, 0.5);

printf("Square root of 5 = %f\n5 to the 1/2 power = 5f", five, onehalf);
return (0);
}```
When i compile:
Code:
```gambit:~>gcc powsqrt5.c -o powsqrt5.c
/tmp/ccwe1kyY.o: In function `main':
powsqrt5.c:(.text+0x3a): undefined reference to `pow'
collect2: ld returned 1 exit status```
I'm using Ubuntu's standard script writer and Linux terminal logged into my universities system as my complier.

4. Originally Posted by symf
with some compilers you have to explicitly link the math.h library. e.g: gcc -o main.exe main.c -lm
Code:
```gambit:~>gcc -o powsqrt5 powsqrt5.c -lm
gcc: powsqrt5.c: No such file or directory
gambit:~>gcc -o powsqrt5.exe powsqrt5.c -lm
gcc: powsqrt5.c: No such file or directory```
I tried and thats what i got... or am i supposed to enter it differently?

5. I can't see you trying the "-lm" in your command line where you compile.
Did you try that?

just a not on the code: I've never seen (doesn't mean it's not possible) writing 'return (0);'. Write just 'return 0;' in the last line of your main program.

6. Originally Posted by symf
I can't see you trying the "-lm" in your command line where you compile.
Did you try that?

just a not on the code: I've never seen (doesn't mean it's not possible) writing 'return (0);'. Write just 'return 0;' in the last line of your main program.
The last thing i have on my command line is "-lm" in both attempts... so yes i did it just gives me the error back that i showed in the post above. And the book Im using to teach me C uses return (0); thats why i use it but thats for the advice.. saves me two key strokes.

7. I had these error calls of not finding math functions. "-lm" did the trick with me. Not sure why your compiler doesn't find the source file at all?...

8. Hmmm. Im pretty sure your right about the comlier not reading in math.h though seeing cbrt(); gives me the same problem;

Code:
```/* This program finds the cube root of 2*/

#include <stdio.h>
#include <math.h>

int main()
{
double cube;

cube = cbrt(2.0);
printf("The cube root of 2 is %f\n", cube);
return 0;
}```
Code:
```gambit:~>gcc cuberoot.c -o cuberoot
/tmp/ccYtRESp.o: In function `main':
cuberoot.c:(.text+0x1b): undefined reference to `cbrt'
collect2: ld returned 1 exit status```

9. I'm out of advice other than try: gcc cuberoot.c -o cuberroot -lm ...

Otherwise, hopefully someone more knowledgeable can help you...

10. Originally Posted by symf
I'm out of advice other than try: gcc cuberoot.c -o cuberroot -lm ...

Otherwise, hopefully someone more knowledgeable can help you...
Thanks for trying atleast!

11. Originally Posted by rhouli67
Code:
```gambit:~>gcc -o powsqrt5 powsqrt5.c -lm
gcc: powsqrt5.c: No such file or directory
gambit:~>gcc -o powsqrt5.exe powsqrt5.c -lm
gcc: powsqrt5.c: No such file or directory```
I tried and thats what i got... or am i supposed to enter it differently?
Looks like you either deleted your source file, or you're not in the directory you think you're in.

12. adding -lm is correct if we assume you are on a linux/unix platform (which it seems like you are, but we could all be wrong). If you are using the Windows version of gcc, then there is probably something else wrong.

When you get this error:
Code:
`gcc: powsqrt5.c: No such file or directory`
that means that your source-code is not available in the current directory. No matter of adding -lm or anything else will help in this case - supplying gcc with the correct path to the actual source-code is the only solution. This is a different problem than having a "can't find _sqrt" - which is definitely caused by not linking with the right library. I usually forget that the first time I use math functions on a new project, so I know the error quite well.

Note that if you didn't include math.h, you would probably not get an error at all, but if you do, it would be something like
Code:
`math.c:5: warning: implicit declaration of function `sqrt'`
However, you only get that if you enable warnings (a good idea) with -Wall on the gcc command line.

--
Mats

13. Originally Posted by matsp
adding -lm is correct if we assume you are on a linux/unix platform (which it seems like you are, but we could all be wrong). If you are using the Windows version of gcc, then there is probably something else wrong.

When you get this error:
Code:
`gcc: powsqrt5.c: No such file or directory`
that means that your source-code is not available in the current directory. No matter of adding -lm or anything else will help in this case - supplying gcc with the correct path to the actual source-code is the only solution. This is a different problem than having a "can't find _sqrt" - which is definitely caused by not linking with the right library. I usually forget that the first time I use math functions on a new project, so I know the error quite well.

Note that if you didn't include math.h, you would probably not get an error at all, but if you do, it would be something like
Code:
`math.c:5: warning: implicit declaration of function `sqrt'`
However, you only get that if you enable warnings (a good idea) with -Wall on the gcc command line.

--
Mats
You were right.. i opened powsqrt5.c and there was nothing in the file. I re-wrote the program in the file and added -lm to the end when i complied and it complied and ran just fine. Thanks for the help!

14. Originally Posted by brewbuck
Looks like you either deleted your source file, or you're not in the directory you think you're in.
You got it. Look at the compilation line from the third post in this thread ..... (I've added colour for emphasis).....

Originally Posted by rhouli67
When i compile:
Code:
```gambit:~>gcc powsqrt5.c -o powsqrt5.c
/tmp/ccwe1kyY.o: In function `main':
powsqrt5.c:(.text+0x3a): undefined reference to `pow'
collect2: ld returned 1 exit status```
That command line attempts to overwrite the source file and, since the link fails with an undefined reference, deletes that source file.

15. Originally Posted by grumpy
That command line attempts to overwrite the source file and, since the link fails with an undefined reference, deletes that source file.
I've done that before

Popular pages Recent additions