我有一个简单的结构:
sample2Make$ ls
ADD DIV inc_print.h main.c makefile multiplication.o printer.o response.o subtraction.o addition.o division.o inc_resp.h main.o MUL printer.c response.c SUB
其中 ADD、DIV、SUB、MUL 是包含操作源文件和头文件的子目录。生成文件是:
app: addition.o subtraction.o multiplication.o division.o response.o
gcc -o app response.o addition.o subtraction.o multiplication.o division.o
response.o: inc_resp.h inc_print.h response.c printer.c main.c
gcc -c response.c printer.c main.c
addition.o: ADD/inc_add.h ADD/addition.c
gcc -c ADD/addition.c
subtraction.o: SUB/inc_sub.h SUB/subtraction.c
gcc -c SUB/subtraction.c
multiplication.o: MUL/inc_mul.h MUL/multiplication.c
gcc -c MUL/multiplication.c
division.o: DIV/inc_div.h DIV/division.c
gcc -c DIV/division.c
头文件只有各自函数的声明。写完命令后:
sample2Make$ make -f makefile
我得到的输出是:
gcc -c ADD/addition.c
gcc -c SUB/subtraction.c
gcc -c MUL/multiplication.c
gcc -c DIV/division.c
gcc -c response.c printer.c main.c
gcc -o app addition.o subtraction.o multiplication.o division.o response.o
/usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu/crt1.o: In function `_start':
/build/buildd/glibc-2.21/csu/../sysdeps/i386/start.S:111: undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:3: recipe for target 'app' failed
make: *** [app] Error 1
最佳答案
您错过了包括 printer.o
和 main.o
在最终的编译语句中,由于缺少对 main()
的引用,你的编译器尖叫。
你的最终陈述应该是这样的
gcc -o app addition.o subtraction.o multiplication.o division.o response.o printer.o main.o
关于c - 未定义对 main、makefile 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34737570/