1

During installation of pintos, I had to run make.

Following is the Makefile.

all: setitimer-helper squish-pty squish-unix

CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o

clean: 
    rm -f *.o setitimer-helper squish-pty squish-unix

In one computer it executed correctly. (output for the command is given below)

gcc -Wall -W   -c -o setitimer-helper.o setitimer-helper.c
gcc -lm  setitimer-helper.o   -o setitimer-helper
gcc -Wall -W   -c -o squish-pty.o squish-pty.c
gcc -lm  squish-pty.o   -o squish-pty
gcc -Wall -W   -c -o squish-unix.o squish-unix.c
gcc -lm  squish-unix.o   -o squish-unix

but in other computer I got the following error

gcc -lm  setitimer-helper.o   -o setitimer-helper
setitimer-helper.o: In function `main':
setitimer-helper.c:(.text+0xc9): undefined reference to `floor'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'setitimer-helper' failed
make: *** [setitimer-helper] Error 1

If looked at first line of outputs of both make commands

gcc -Wall -W   -c -o setitimer-helper.o setitimer-helper.c

and

gcc -lm  setitimer-helper.o   -o setitimer-helper

They are different.

Why make is executing different commands for the same Makefile? and What should I do to remove error?

sai
  • 59
  • 4
  • 1
    Looks like, in the second computer, some built binaries were already there. Is your working directory a shared partition mounted on both computers? Or did you `make` on the first and then copy everything, including binaries, on the second? Are the two computers identical (same hardware and same installed software)? – Renaud Pacalet Aug 04 '17 at 13:18

1 Answers1

1

In the first computer, the setitimer-helper.o file either doesn't exist or the setitimer-helper.c file is newer, so make needs to rebuild it. Thus it runs the compiler, then afterwards it performs the link operation:

gcc -Wall -W   -c -o setitimer-helper.o setitimer-helper.c
gcc -lm  setitimer-helper.o   -o setitimer-helper

On the second computer, the setitimer-helper.o file already exists and is newer than the setitimer-helper.c file, so the compile command was not needed and the second computer proceeded directly to the link line:

gcc -lm  setitimer-helper.o   -o setitimer-helper

The real question is why you got the linker error on the second computer.

The answer to that is that the -lm flag needs to come on the linker line after the object files. This happens because you added -lm to the LDFLAGS variable which is not the right one: that should contain options that tell the linker where to look for files, etc. (for example, the -L option).

Libraries should be added to the LDLIBS variable, not LDFLAGS. Change your makefile to this:

all: setitimer-helper squish-pty squish-unix

CC = gcc
CFLAGS = -Wall -W
LDLIBS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o

clean: 
        rm -f *.o setitimer-helper squish-pty squish-unix

Your link line will then look like:

gcc  setitimer-helper.o   -o setitimer-helper -lm

and should work properly.

MadScientist
  • 92,819
  • 9
  • 109
  • 136