这个makefile有什么问题,如何修复?
我有4个文件:pass1.c、pass2.c、main.c和header1.h 每个文件都包括文件头.h 我编写了以下makefile:这个makefile有什么问题,如何修复?,makefile,Makefile,我有4个文件:pass1.c、pass2.c、main.c和header1.h 每个文件都包括文件头.h 我编写了以下makefile: assembler: pass1.o pass2.o main.o gcc pass1.o pass2.o -o assembler pass1.o: pass1.c gcc -c -ansi -Wall -pedantic pass1.c -o pass1.o pass2.o: pass2.c gcc -c -a
assembler: pass1.o pass2.o main.o
gcc pass1.o pass2.o -o assembler
pass1.o: pass1.c
gcc -c -ansi -Wall -pedantic pass1.c -o pass1.o
pass2.o: pass2.c
gcc -c -ansi -Wall -pedantic pass2.c -o pass2.o
main.o: main.c
gcc -c -ansi -Wall -pedantic main.c -o main.o
当我确实犯了错误时,我得到了以下错误:
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'assembler' failed
make: *** [assembler] Error 1
请注意,我没有编写名为“start”的函数
这里的问题是什么?如何解决
gcc pass1.o pass2.o -o assembler
应该是
gcc main.o pass1.o pass2.o -o assembler
将main.c
重命名为assembler.c
,整个makefile可以压缩为
CFLAGS := -ansi -Wall -pedantic
objects := assembler.o pass1.o pass2.o
assembler: $(objects)
$(objects): header.h
假设系统上的
cc
是指向gcc
的符号链接,显然,在任何对象文件中都没有函数main
。@tofro我确实在项目中看到了main.o文件。您没有链接到该对象文件。另外,您通常希望在链接行的开头包含main
的文件。如果没有,您可能会得到其他未定义的符号。