Continue to Site

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.

  • 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.

Gnu c math.h pow bug

Status
Not open for further replies.

Inquisitive

Super Moderator
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.
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:
//    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!
 
I think you should put the -lm at the end of the command. This is due to the mechanics how makefile works.
 
First running "make clean"

Editing the makefile to
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.
 
Could you post the full output of the make.
 
Like this?

Code:
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$
 
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:
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:
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
 
Actually, try if this simpler version works..
(Sorry, I'm rustier with makefiles than I thought.)

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]
 
Which produces

Code:
lepo@ubulap:~/DeitelC/Ch04$ make
makefile:5: *** missing separator.  Stop.
lepo@ubulap:~/DeitelC/Ch04$
 
Yeah, the syntax is not spot on. The indentations need to be tabs.. there might be something else also.
 
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:
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$
 
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.
 
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.
 
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.

 
Code:
CC=gcc
LDFLAGS= -lm
SOURCES=test.c
BIN=test

all: $(BIN)
   
$(BIN): $(SOURCES)
    $(CC) $(SOURCES) -o $(BIN) $(LDFLAGS)
 
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.
 
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:
Thanks Styx.. :)
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top