在Ubuntu 11.10中使用gcc链接错误 [英] Linking error using gcc in Ubuntu 11.10

查看:169
本文介绍了在Ubuntu 11.10中使用gcc链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C中编译和运行一个简单的Hello World程序,并且我得到了一个奇怪的错误
,其中包含一些C stdlib函数的多重定义。



.c文件是:

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

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

printf(hello world\\\
);
返回0;
}

我用来编译和创建可执行文件的命令如下:

  gcc -Werror -Wall src / main.c -o bin / mo 
gcc -Werror -Wall bin / mo -o bin / m

编译部分没问题,但在第二个命令中,我调用GCC Linker
创建一个可执行文件我得到以下错误:

$ p $ bin / mo:在函数`_fini'中:
(.fini + 0x0):`_fini'的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/在函数`__data_start'中:
(.data + 0x0):`__data_start'的多重定义
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位0具有无效的符号索引10
/ usr / bin / ld:/ usr / bin / usr / lib / debug / usr / lib / x86_64-linux-gnu / crt1.o(.debug_info):重定位1的符​​号索引无效11
/ usr / bin / ld:/ usr / lib / debug / usr / LIB / x86_64的-Linux的gnu / crt1.o(.debug_info):重定位2具有无效的符号索引2
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(。 debug_info):重定位3具有无效的符号索引2
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位4无效符号索引10
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位5具有无效的符号索引12
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位6具有无效的符号索引12
/ usr / bin / ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位7具有无效的符号索引12
/ usr / bin / ld:/ usr / lib / debug / usr / lib / x86_64-linux-gnu / crt1.o(.debug_info):重定位8具有无效的符号索引2
/ usr / bin / ld:/ usr / lib / debug / usr / lib / x86_64-linux -gnu / crt1.o(.debug_info):重定位9具有无效的符号索引2
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o( .debug_info):搬迁10有我无效符号索引11
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位11具有无效符号索引12
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位12具有无效的符号索引12
/ usr / bin / ld :/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位13有无效的符号索引12
/ usr / bin / ld:/ usr / lib / debug /usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位14具有无效的符号索引12
/ usr / bin / ld:/ usr / lib / debug / usr / lib / x86_64- linux-gnu / crt1.o(.debug_info):重定位15具有无效的符号索引12
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o (.debug_info):重定位16具有无效的符号索引12
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位17具有无效的符号索引12
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位18具有无效的符号索引12
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重定位19有无效的符号索引12
/ usr / bin / ld:/ usr /lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重新定位20的符号索引无效19
/usr/lib/gcc/x86_64-linux-gnu/4.6.1 /../../../x86_64-linux-gnu/crt1.o:(.data+0x0):首先在这里定义
bin / mo:在函数`__data_start'中:
(。 data + 0x8):`__dso_handle'的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/crtbegin.o:(.data+0x0):首先在这里定义
bin / mo :(。rodata + 0x0):`_IO_stdin_used'的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux -gnu / crt1.o:(.rodata.cst4 + 0x0):首先定义在这里
bin / mo:在函数`_start'中:
(.text + 0x0):多重定义`_start'
/ usr / bin / ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line):重定位0具有无效的符号索引2
/ usr / LIB / GCC / x86_64的-Linux的GNU / 4.6.1 /../../../ x86_64的-Linux的GNU / CRT 1.o:/ build / buildd /eglibc-2.13/csu/../sysdeps/x86_64/elf/start.S:109:首先在这里定义
bin / mo:在函数`_init'中:
(.init + 0x0):`_init'的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/ crti.o :( .init + 0x0):首先在这里定义
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/crtend.o:(.dtors+0x0):多重定义` __DTOR_END__'
bin / mo :(。dtors + 0x8):首先在这里定义
/ usr / bin / ld:bin / mo(.eh_frame)中的错误;没有.eh_frame_hdr表将被创建。
collect2:ld返回1退出状态

顺便说一句,当我运行mo文件使用bash执行前缀,它工作得很好



从来没有在C或其他任何程序中进行编程,我试着开始,但是这让我回到了

解决方案

在编译步骤中需要 -c ,否则你的 mo 对象将是一个完全链接的可执行文件。示例:

  gcc -Werror -Wall -c src / main.c -o bin / mo 
gcc -Werror -Wall bin / mo -o bin / m

如果您的程序是由不仅仅是单个翻译单元。如果您有 file1.c file2.c ,例如:

  gcc -c file1.c -o file1.o 
gcc -c file2.c -o file2.o
gcc file1.o file2。 o -o app

是正确的,而如果您有:

  gcc file1.c -o file1.o 
gcc file2.c -o file2.o
gcc file1.o file2.o -o app

第一行和第二行都会在您进入最后一个链接阶段之前失败,因为它们会丢失符号。


I'm trying to compile and run a simple "Hello World" program in C, and I get a strange error of multiple definition of some C stdlib functions

The .c file is:

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

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

  printf("hello world\n");
  return 0;
}

the commands I use to compile and create an executable are:

gcc -Werror -Wall src/main.c -o bin/m.o
gcc -Werror -Wall bin/m.o -o bin/m

The compilation part goes fine, but in the second command, where I invoke the GCC Linker to create an executable I get the following errors:

bin/m.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
bin/m.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/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.1/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
bin/m.o: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/crtbegin.o:(.data+0x0): first defined here
bin/m.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crt1.o:             (.rodata.cst4+0x0): first defined here
bin/m.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crt1.o:/build/buildd    /eglibc-2.13/csu/../sysdeps/x86_64/elf/start.S:109: first defined here
bin/m.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first     defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/crtend.o:(.dtors+0x0): multiple definition of     `__DTOR_END__'
bin/m.o:(.dtors+0x8): first defined here
/usr/bin/ld: error in bin/m.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status

by the way, when I run the m.o file with the bash executing prefix, it works just fine

never programed in C or anything, I'm trying to start, but this holds me back

解决方案

You need a -c in the compile step, or else your m.o object will be a fully linked executable. Example:

gcc -Werror -Wall -c src/main.c -o bin/m.o
gcc -Werror -Wall bin/m.o -o bin/m

The difference would be immediately obvious if your program was composed of more than just the single translation unit. If you had file1.c and file2.c, for example:

gcc -c file1.c -o file1.o
gcc -c file2.c -o file2.o
gcc file1.o file2.o -o app

Is correct, whereas if you had:

gcc file1.c -o file1.o
gcc file2.c -o file2.o
gcc file1.o file2.o -o app

Both the first and second lines would fail before you even got to the final link stage, since they would be missing symbols.

这篇关于在Ubuntu 11.10中使用gcc链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆