首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同一个Makefile在不同的计算机上执行不同的命令

同一个Makefile在不同的计算机上执行不同的命令
EN

Stack Overflow用户
提问于 2017-08-04 13:05:10
回答 1查看 374关注 0票数 1

在安装pintos期间,我不得不运行make

下面是Makefile。

代码语言:javascript
复制
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

在一台计算机中,它正确地执行。(命令的输出如下所示)

代码语言:javascript
复制
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

但在另一台计算机中,我得到了以下错误

代码语言:javascript
复制
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命令的第一行输出

代码语言:javascript
复制
gcc -Wall -W   -c -o setitimer-helper.o setitimer-helper.c

代码语言:javascript
复制
gcc -lm  setitimer-helper.o   -o setitimer-helper

它们是不同的。

为什么make对同一个Makefile执行不同的命令?我该怎么做才能消除错误呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-04 18:22:34

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

代码语言:javascript
复制
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文件更新,因此不需要编译命令,第二台计算机直接进入链接线:

代码语言:javascript
复制
gcc -lm  setitimer-helper.o   -o setitimer-helper

真正的问题是为什么在第二台计算机上有链接器错误。

答案是,-lm标志需要出现在对象文件之后的链接器行上。之所以会出现这种情况,是因为您将-lm添加到LDFLAGS变量(这不是正确的变量):该变量应该包含告诉链接器在哪里查找文件的选项(例如,-L选项)。

库应该添加到LDLIBS变量,而不是LDFLAGS。将makefile更改为:

代码语言:javascript
复制
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

然后,您的链接线将类似于:

代码语言:javascript
复制
gcc  setitimer-helper.o   -o setitimer-helper -lm

而且应该正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45507638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档