Same Makefile executing different commands in different computers

时间:2017-08-04 13:05:11

标签: makefile pintos

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?

1 个答案:

答案 0 :(得分:1)

在第一台计算机中,setitimer-helper.o文件不存在或setitimer-helper.c文件较新,因此需要重建它。因此它运行编译器,然后执行链接操作:

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

在第二台计算机上,setitimer-helper.o文件已存在并且比setitimer-helper.c文件更新,因此不需要编译命令,第二台计算机直接进入链接行:

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

真正的问题是你在第二台计算机上遇到链接器错误的原因。

答案是在目标文件之后,-lm标志需要在链接器行上。发生这种情况是因为您将-lm添加到LDFLAGS变量中,而该变量不是正确的:它应该包含告诉链接器在哪里查找文件的选项等(例如,{{1} }选项)。

应将库添加到-L变量,而不是LDLIBS。将您的makefile更改为:

LDFLAGS

您的链接行将如下所示:

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

并且应该正常工作。