: undefined reference to `pow'

This is a discussion on : undefined reference to `pow' within the C Programming forums, part of the General Programming Boards category; Hi all, I am playing with pow() function using gcc compiler. I have got some intresting error while playing. Here ...

  1. #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.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    5
    Sorry i did'nt think that these are the answers of my questions

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    5
    Sorry that was an accident

  6. #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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #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

  9. #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

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    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.
    Last edited by laserlight; 02-09-2010 at 07:07 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  12. #12
    TJz
    TJz is offline
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    Code:
    gcc -o test test.c -lm
    U always need to use -lm when using such functions as "pow" or "exp", as u need to create a link. This is not an error, it's just the way gcc compiler works.

    for details do

    Code:
    man pow

  13. #13
    TJz
    TJz is offline
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    BTW, pow is defined in <math.h> so u should include it too

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Not a lot that the OP didn't know about 6 months ago.
    Closed - and please refrain from bumping dead threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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