我有以下文件:
main.c中
#include "other.h"
int main(int argc, char *argv[]) {
other();
return 0;
}
other.c
#include "other.h"
void other(void) {
1+1;
}
other.h
#ifndef OTHER_H
#define OTHER_H
void other(void);
#endif
我正在使用这个makefile:
OBJ = other.o main.o
main: $(OBJ)
gcc $(OBJ) -o $@
main.o: main.c
gcc -c main.c
other.o: other.c
gcc -c other.c
clean:
rm -f $(OBJ) main
当我运行 make clean && make
时,所有内容都会成功编译和链接 .
然后,我将空格更改为 other.c
.
现在,当我运行 make
时,我收到以下错误:
gcc other.o main.o -o main
main.o: In function `main':
main.c:(.text+0x5): undefined reference to `other'
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'main' failed
make: *** [main] Error 1
更新 other.c
后为什么链接失败?
请注意,如果我将空格更改为 main.c
,或者如果我再次运行 make clean
,则链接有效 .
更新:如果它有用,我将工作案例中的other.o文件与破损案例中的other.o文件进行比较;他们有一点点差异 . 我不知道是什么会导致这种差异,也不知道如何解释它 . 见图:difference between other.o, working case and broken case
1 回答
好吧,我想我想出来了 . 它似乎正在发生,因为我在我的文本编辑器中有linter(Atom版本1.33,linter版本2.2.0,linter-gcc版本0.7.1) .
当我在Atom中更新文件other.c并保存时,文件
other.o
也会通过linter更新 . 如果我比较other.o
中的符号,在它包含other_function
之前,但在它包含_Z14other_functionv
之后 .other.o
上更新的时间戳也解释了为什么make
没有运行gcc -c other.c
.当我使用不同的文本编辑器更改空白时,
make
正常工作 .