pow(x, y); problems

This is a discussion on pow(x, y); problems within the C Programming forums, part of the General Programming Boards category; This program is supposed to output the Power of 2's Table: Code: #include <stdio.h> #include <math.h> int main() { double ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    20

    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. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    with some compilers you have to explicitly link the math.h library. e.g: gcc -o main.exe main.c -lm

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    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.
    Last edited by rhouli67; 02-16-2009 at 03:28 PM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by symf View Post
    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. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    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.
    Last edited by symf; 02-16-2009 at 03:38 PM.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by symf View Post
    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. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    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. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    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. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    I'm out of advice other than try: gcc cuberoot.c -o cuberroot -lm ...

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

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by symf View Post
    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. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by rhouli67 View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by matsp View Post
    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. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by brewbuck View Post
    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).....

    Quote Originally Posted by rhouli67 View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by grumpy View Post
    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
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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