gpt4 book ai didi

C 生成文件错误 : ld returned 1 exit

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:32 25 4
gpt4 key购买 nike

我的老师不是最擅长解释 C 的,所以我在理解 makefile 的连接时遇到了一些麻烦。我已经添加了 complex.c、complex.h 和 main.c 的代码。我只是在使用 make 命令编译它时遇到了麻烦。我按照他提供的 powerpoint 上的示例进行了操作,但我不明白为什么它无法变得复杂。

生成文件

complex: main.o complex.o
gcc -o complex main.o complex.o
main.o: main.c complex.h
gcc -c main.c -lm
complex.o: complex.c complex.h
gcc -c complex.c -lm
clean:
rm*.o complex
ls

主.o

main.o: complex.h
gcc -c main.c

复杂.o

complex.o: complex.h
gcc -c complex.c

错误

mason% make
gcc -o complex main.o complex.o
ld: fatal: file main.o: unknown file type
ld: fatal: file processing errors. No output written to complex
collect2: error: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `complex'

最佳答案

看起来您已将 Makefile 片段放入 main.ocomplex.o 中。这些应该由编译器生成,而不是您

删除这些文件,然后再次make

此外,您的 make clean 规则缺少一个空格。

clean:
rm *.o complex
ls

还有一件事。编译行中不需要 -lm

main.o: main.c complex.h
gcc -c main.c
complex.o: complex.c complex.h
gcc -c complex.c

您应该在链接阶段添加-lm

complex: main.o complex.o
gcc -o complex main.o complex.o -lm

关于C 生成文件错误 : ld returned 1 exit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com