在安装pintos期间,我不得不运行make。
下面是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在一台计算机中,它正确地执行。(命令的输出如下所示)
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但在另一台计算机中,我得到了以下错误
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如果查看两个make命令的第一行输出
gcc -Wall -W -c -o setitimer-helper.o setitimer-helper.c和
gcc -lm setitimer-helper.o -o setitimer-helper它们是不同的。
为什么make对同一个Makefile执行不同的命令?我该怎么做才能消除错误呢?
发布于 2017-08-04 18:22:34
在第一台计算机中,setitimer-helper.o文件要么不存在,要么setitimer-helper.c文件更新,因此make需要重新构建它。因此,它运行编译器,然后执行链接操作:
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变量(这不是正确的变量):该变量应该包含告诉链接器在哪里查找文件的选项(例如,-L选项)。
库应该添加到LDLIBS变量,而不是LDFLAGS。将makefile更改为:
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然后,您的链接线将类似于:
gcc setitimer-helper.o -o setitimer-helper -lm而且应该正常工作。
https://stackoverflow.com/questions/45507638
复制相似问题