从来没系统的看过makefile文档,平时属于复制模板,用完即忘,下午尝试按自己的理解写一个最简单的makefile,含2个.c文件,1个.h文件,费了个把小时,参考别人的文章才弄出来,特记录。
main.c:
1 #include2 #include "command.h" 3 4 int main(int argc, const char *argv[]) 5 { 6 printf("run in main\n"); 7 command(); 8 return 0; 9 }
commad.c:
1 #include2 #include "command.h" 3 4 void command(void) 5 { 6 printf("run in command\n"); 7 }
commad.h:
1 #ifndef __COMMAND_H__ 2 #define __COMMAND_H__ 3 4 void command(void); 5 6 #endif
makefile:
1 objects = main.o command.o 2 3 main:$(objects) 4 gcc $(objects) -o main 5 6 main.o:main.c command.h 7 gcc main.c -o main.o 8 9 command.o:command.c command.h 10 gcc command.c -o command.o 11 12 13 .PHONY:clean 14 clean: 15 rm main $(objects)
make之后,报错:
gcc main.c -o main.o
/tmp/ccTBTS4t.o:在函数‘main’中:
main.c:(.text+0x16):对‘command’未定义的引用
collect2: error: ld returned 1 exit status
make: *** [main.o] 错误 1
于是开始了我的第一个错误理解,我的makefile出错在6-7行,执行生成目标main.o时,我看到错误,认为是我的目标依赖应该要包含commad.o,因为我的main.c调用了command.c里面的函数。于是我为main.o添加依赖commad.o,
1 objects = main.o command.o 2 3 main:$(objects) 4 cc $(objects) -o main 5 6 main.o:main.c command.o command.h 7 gcc main.c command.o -o main.o 8 9 command.o:command.c command.h 10 gcc command.c -o command.o 11 12 13 .PHONY:clean 14 clean: 15 rm main $(objects)
看到6-7行,觉得超不对劲,姑且不管,再次make,由于依赖关系,推导执行到command.o:
gcc command.c -o command.o
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o:在函数‘_start’中:
(.text+0x18):对‘main’未定义的引用
collect2: error: ld returned 1 exit status
make: *** [command.o] 错误 1
看到了/usr/bin/ld,没错,就是进行了链接操作,也就是生成可执行文件的最后一步,问题是我这里只不过是想把command.c生成目标文件command.o。于是怀疑gcc command.c -o command.o这个格式不正确。回忆下一个可执行文件的生成过程:预编译,汇编(对高级于汇编语言的才有),编译,链接。而我只想它停在编译后。将这几个步骤拆分:
预编译:
gcc -E a.c -o a.i
汇编:
gcc -S a.i -o a.s
编译:
gcc -c a.s -o a.o
链接:
gcc a.o -o a
其实最后这个链接还可以细分,比如这里会将command.o main.o ,libc库(我用了printf),还有就是链接加载库,一起链接成可执行文件。
于是我的错误就漏了 “-c” ,补上:
1 objects = main.o command.o 2 3 main:$(objects) 4 cc $(objects) -o main 5 6 main.o:main.c command.h 7 gcc -c main.c -o main.o 8 9 command.o:command.c command.h 10 gcc -c command.c -o command.o 11 12 13 .PHONY:clean 14 clean: 15 rm main $(objects)
main.c中调用了command.c里面的函数也并不需要将其作为main.o的依赖,因为main.o中调用command()函数会留一个符号表在main.o文件中,直到最后进行链接的时候才确定其具体值。