我有以下文件:
主c

#include "other.h"

int main(int argc, char *argv[]) {
    other();
    return 0;
}

其他c
#include "other.h"

void other(void) {
    1+1;
}

其他.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,则链接有效。
更新:如果有用的话,我将工作案例中的另一个.o文件与断开案例中的另一个.o文件进行了比较;它们略有不同我不知道什么会导致这种差异,也不知道如何解释见图片:difference between other.o, working case and broken case

最佳答案

好吧,我想我已经知道了这似乎是因为我的文本编辑器中有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工作正常。

08-05 21:51
查看更多