C程序链接错误:是什么导致这些?

时间:2014-09-11 09:49:01

标签: c linux linker-errors

它编译,但它不会从.o文件链接到可执行文件。我已经把我现在和它运作良好的一切都改变了(基本上,str_to_int中的所有内容)。

我的代码:

#include <stdio.h>
#include <stdlib.h>

int str_to_int( char string[] ) {
   // takes null-terminate ascii string, returns int value
   // value of digit at i = (value*10)+(str[i]-'0').
   int num=0;
   int sign=0;

   return num;
}

int main( int argc, char* argv[] ) {

   if (argc==1) {
      printf( "usage: review_grades score1 [score2 ...]\n" );
      printf( "note: score values are non-negative.\n" );
   }
   else {
      printf( "population: %u \n", argc-1 ); // population
      printf( "%u \n", str_to_int(argv[1]));
   }

   return EXIT_SUCCESS ;
}

它产生的错误:

myname@machine:~/path/folder$ gcc -c -std=c99 review_grades.c
myname@machine:~/path/folder$ gcc -o -std=c99 rev review_grades.o
rev: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): firs                                                                                   t defined here
rev: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/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): relocati                                                                                on 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 10 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 21 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocati                                                                                on 22 has invalid symbol index 21
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.data+0x0): firs                                                                                t defined here
rev: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
rev:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
rev: In function `_start':
(.text+0x0): multiple definition of `_start'
/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.6/../../../i386-linux-gnu/crt1.o:/build/buildd/eglibc-2.15/csu/../sysdeps/i386/elf/start.S:119: first defined here
rev:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here
rev: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
review_grades.o: In function `str_to_int':
review_grades.c:(.text+0x0): multiple definition of `str_to_int'
rev:(.text+0xb4): first defined here
review_grades.o: In function `main':
review_grades.c:(.text+0x19): multiple definition of `main'
rev:(.text+0x11b): first defined here
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
rev:(.dtors+0x4): first defined here
/usr/bin/ld: error in rev(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status

我做错了什么?

2 个答案:

答案 0 :(得分:5)

您不小心在<{em} -std=c99及其参数-o之间插入了rev选项

换句话说,而不是:

-o -std=c99 rev

你应该有这个:

-std=c99 -o rev

或者这个:

-o rev -std=c99

但是,具有-std=个文件作为输入的命令不需要.o选项,因此您也可以完全删除它。

错误消息是因为该命令已将rev解释为输入文件,但由于它已被链接(可能是在之前的构建尝试中),因此无法正常工作,并且您无法重新链接没有重复符号的可执行文件(不管怎么说)。

答案 1 :(得分:0)

在-o之后你应该写 output_file 名称

myname@machine:~/path/folder$ gcc -c -std=c99 review_grades.c
myname@machine:~/path/folder$ gcc -o rev review_grades.o