未定义的对main,makefile的引用

时间:2016-01-12 07:15:34

标签: c makefile

我有一个简单的结构:

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是包含操作源文件和头文件的子目录。 makefile是:

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

1 个答案:

答案 0 :(得分:2)

您错过了在最终编译语句中包含printer.omain.o,并且由于缺少对main()的引用,您的编译器会尖叫。

您的最终陈述应该是

gcc -o app addition.o subtraction.o multiplication.o division.o response.o printer.o main.o