Makefile抛出重定位错误

时间:2014-12-02 16:29:14

标签: c makefile

我正在尝试编写一个makefile来编译几个相互依赖的文件。文件结构如下:

- ext2.h
- ext_helper.c
- ext2_cp.c
- ext2_ln.c
- ext2_mkdir.c
- ext2_rm

每个源文件都依赖于ext2.h头文件。最后四个文件彼此独立,但依赖于ext2_helper.c的某些辅助函数。

我试图为这种情况编写一个makefile,如下所示:

ext2_cp :  ext2_helper.o
    gcc -Wall -g -o ext2_cp $^

ext2_mkdir :  ext2_helper.o
    gcc -Wall -g -o ext2_mkdir $^

ext2_ln :  ext2_helper.o
    gcc -Wall -g -o ext2_ln $^

ext2_rm :  ext2_helper.o
    gcc -Wall -g -o ext2_rm $^

%.o : %.c ext2.h
    gcc -Wall -g -c $<

clean : 
    rm *.o ext2_cp ext2_mkdir ext2_ln ext2_rm

但是,在运行这个makefile时,我收到了几个重定位错误,如下所示:

make
gcc -Wall -g -c ext2_helper.c
gcc -Wall -g -o ext2_cp ext2_helper.o
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [ext2_cp] Error 1

我已经查看了其他一些有相同问题的帖子,但并不完全了解错误的原因。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

错误消息说明了一切:您尝试创建没有main()的可执行文件。您似乎忘记了依赖项列表中的某些内容。

试试这个:

ext2_cp: ext2_cp.o ext2_helper.o
    gcc -Wall -g -o ext2_cp $^

ext2_mkdir: ext2_mkdir.o ext2_helper.o
    gcc -Wall -g -o ext2_mkdir $^

ext2_ln: ext2_ln.o ext2_helper.o
    gcc -Wall -g -o ext2_ln $^

ext2_rm: ext2_rm.o ext2_helper.o
    gcc -Wall -g -o ext2_rm $^

%.o: %.c ext2.h
    gcc -Wall -g -c $<

clean: 
    rm *.o ext2_cp ext2_mkdir ext2_ln ext2_rm