Search code examples
makefilepintos

Same Makefile executing different commands in different computers


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?


Solution

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