1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Gnu c math.h pow bug

Discussion in 'Computers and Networks' started by Inquisitive, Oct 11, 2013.

  1. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    I'm playing with GNU C on Ubuntu 12.04 LTS on a 64bit system.

    When I compile the program using a makefile I get an error:
    "undefined reference to `pow'
    collect2: ld returned 1 exit status"

    But if compiled with the same command line that is used in the makefile but use it in a terminal window it compiles without errors and functions normally. What is going on here?

    Here is the makefile.
    Code (makefile):
    # build an executable named fig04_06 from fig04_06.c
    all: fig04_06
        gcc -g -Wall fig04_06.c -lm -o fig04_06

    clean:
        $(RM) fig04_06
    Here is the C program that is giving the math.h pow error.
    Code (GNU C):
    //    Fig. 4.6:    fig04_06.c
    //    Calculating compound interest
    #include <stdio.h>
    #include <math.h>

    //    function main begins program execution
    int main( void )
    {
        double amount;    // amount on deposit
        double principal = 1000.0;    //    starting principal
        double rate = .05;    //    annual interest rate
        unsigned int year;    //    year counter

        //    output table column heads
        printf( "%4s%21s\n", "Year", "Amount on deposit" );

        //    calculate amount on depposit for each of ten years
        for ( year = 1; year <= 10; ++year ) {

        //    calculate new amount for specified year
        amount = principal * pow( 1.0 + rate, year );

        //    output one table row
        printf( "%4u%21.2f\n", year, amount );
        }    //    end for

        return 0;
    }    //    end function main
     
    Any ideas as to how to solve this?

    Thanks!
     
  2. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I think you should put the -lm at the end of the command. This is due to the mechanics how makefile works.
     
  3. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    First running "make clean"

    Editing the makefile to
    Code (makefile):
    gcc -g -Wall fig04_06.c -o fig04_06 -lm
    Produces same error
    "undefined reference to `pow'
    collect2: ld returned 1 exit status"

    However executing the statement gcc -g -Wall fig04_6.c -o fig04_06 -lm in a terminal window command line produces no errors and compiles as expected.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    Could you post the full output of the make.
     
  6. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    Like this?

    Code (makefile output):
    lepo@ubulap:~/DeitelC/Ch04$ make
    cc    fig04_06.c  -o fig04_06
    /tmp/ccs1glMa.o: In function `main':
    fig04_06.c:(.text+0x87): undefined reference to `pow'
    collect2: ld returned 1 exit status
    make: *** [fig04_06] Error 1
    lepo@ubulap:~/DeitelC/Ch04$
     
     
  7. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Maybe the problem is that you are trying to use Makefile as a script. It is not meant to work that way.

    This is a nice clean template for a makefile.
    Code (text):

    CC = gcc
    CFLAGS =-Wall-W
    LDFLAGS =-lm

    myprog: myprog.o more_code.o
           ${CC} ${CFLAGS} myprog.o more_code.o ${LDFLAGS}-o myprog

    myprog.o: myprog.c
           ${CC} ${CFLAGS}-c myprog.c

    more_code.o: more_code.c
           ${CC} ${CFLAGS}-c more_code.c

    clean:
           \rm myprog.o more_code.o myprog
     
    For you the file is something like:

    Code (text):

    CC = gcc
    CFLAGS =-Wall-W
    LDFLAGS =-lm

    fig04_06: fig04_06.o
           ${CC} ${CFLAGS} fig04_06.o ${LDFLAGS}-o fig04_06

    fig04_06.o: fig04_06.c
           ${CC} ${CFLAGS}-c fig04_06.c

    clean:
           \rm fig04_06.o fig04_06
     
     
  8. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Actually, try if this simpler version works..
    (Sorry, I'm rustier with makefiles than I thought.)

    Code (makefile):
    # build an executable named fig04_06 from fig04_06.c
    all: fig04_06

    fig04_06: fig04_06.c
        gcc -o fig04_06 fig04_06.c -g -Wall -I -lm

    clean:
        $(RM) fig04_06
    [/quote]
     
  9. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    Which produces

    Code (makefile output):
    lepo@ubulap:~/DeitelC/Ch04$ make
    makefile:5: *** missing separator.  Stop.
    lepo@ubulap:~/DeitelC/Ch04$
     
  10. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Yeah, the syntax is not spot on. The indentations need to be tabs.. there might be something else also.
     
  11. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    yes, it was a tab needed in the makefile before the gcc statement.

    make file runs now but still have this error to replace it on make

    Code (makefile output):
    lepo@ubulap:~/DeitelC/Ch04$ make
    gcc -o fig04_06 fig04_06.c -g -Wall -I -lm
    /tmp/ccXuFfFV.o: In function `main':
    /home/lepo/DeitelC/Ch04/fig04_06.c:21: undefined reference to `pow'
    collect2: ld returned 1 exit status
    make: *** [fig04_06] Error 1
    lepo@ubulap:~/DeitelC/Ch04$
     
  12. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Well, maybe the -o needs to be after -lm:

    gcc fig04_06 fig04_06.c -g -Wall -I -lm -o

    ... this is driving me nuts :) I don't think it is a library issue though. It should be able to find and access the library ok.
     
  13. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    gcc fig04_06 fig04_06.c -g -Wall -I -lm -o
    gcc: error: fig04_06: No such file or directory
    gcc: error: missing filename after ‘-o’
    make: *** [fig04_06] Error 1

    It doesn't like that.
     
  14. Styx

    Styx Active Member

    Joined:
    Dec 19, 2003
    Messages:
    1,743
    Likes:
    9
    Location:
    UK
    nvm..
     
  15. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Oh the horror.. I don't know one single programmer that likes makefile.. it is great tool, but not when you have to write it yourself. Usually an IDE creates the file for you. If this is going to be a lot larger project, get an IDE.. or wait until some linux guru finds this post.. or work it out.

     
  16. Styx

    Styx Active Member

    Joined:
    Dec 19, 2003
    Messages:
    1,743
    Likes:
    9
    Location:
    UK
    Code (text):
    CC=gcc
    LDFLAGS= -lm
    SOURCES=test.c
    BIN=test

    all: $(BIN)
       
    $(BIN): $(SOURCES)
        $(CC) $(SOURCES) -o $(BIN) $(LDFLAGS)
     
    • Like Like x 2
  17. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    Problem solved. That works! The makefile now functions as it should and it compiles the program error free.

    I substituted the words "test" and "test.c" with my actual programs name in the makefile.

    Don't understand why it works, but it works.

    Thanks Styx and misterT.

    Help much appreciated.
     
  18. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    922
    Likes:
    86
    Location:
    Georgia, USA
    Let me ask this then. Right now that makefile must be edited in two places in order for it to work with the next file that is to be compiled. That being the working program name. Can a variable be used inside the makefile that would accept the program name from command line that would then in turn place it into the makefile? So it would not have to be edited each time. Secondly any good tutorials on makefile that you would recommend?
     
    Last edited: Oct 13, 2013
  19. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Thanks Styx.. :)
     

Share This Page