直接与LD链接一个C程序失败,未定义的引用`__libc_csu_fini` [英] Linking a C program directly with ld fails with undefined reference to `__libc_csu_fini`

查看:4504
本文介绍了直接与LD链接一个C程序失败,未定义的引用`__libc_csu_fini`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译Linux下的C程序。然而,出于好奇,我试图用手来执行一些步骤:我使用的:

I'm trying to compile a C program under Linux. However, out of curiosity, I'm trying to execute some steps by hand: I use:


  • gcc的前端,产生汇编code

  • 然后运行GNU汇编获取对象文件

  • 然后再与C运行时链接它得到一个工作的可执行文件。

现在我被困在连接的部分。

Now I'm stuck with the linking part.

该计划是一个非常基本的Hello world:

The program is a very basic "Hello world":

#include <stdio.h>
int main() {
   printf("Hello\n");
   return 0;
}

我用下面的命令来生产组装code:

I use the following command to produce the assembly code:

gcc hello.c -S -masm=intel

我要告诉GCC编译后退出并转储组装code。与英特尔的语法。

I'm telling gcc to quit after compiling and dump the assembly code with Intel syntax.

然后我用个GNU汇编器生成的对象文件:

Then I use th GNU assembler to produce the object file:

as -o hello.o hello.s

然后我用LD来产生最终的可执行尝试:

Then I try using ld to produce the final executable:

ld hello.o /usr/lib/libc.so /usr/lib/crt1.o -o hello

不过,我不断收到以下错误消息:

But I keep getting the following error message:

/usr/lib/crt1.o: In function `_start':
(.text+0xc): undefined reference to `__libc_csu_fini'
/usr/lib/crt1.o: In function `_start':
(.text+0x11): undefined reference to `__libc_csu_init'

符号 __ libc_csu_fini /初始化似乎是glibc的一部分,但我不能在任何地方找到他们!我试着用同样的结果静态链接的libc(对 /usr/lib/libc.a )。

The symbols __libc_csu_fini/init seem to be a part of glibc, but I can't find them anywhere! I tried linking against libc statically (against /usr/lib/libc.a) with the same result.

还有什么问题呢?

推荐答案

我发现<一个href=\"http://stackoverflow.com/questions/5821237/linking-a-dynamically-linked-executable-with-ld/6656952#6656952\">another帖子其中载有线索: -dynamic-连接/lib/ld-linux.so.2

I found another post which contained a clue: -dynamic-linker /lib/ld-linux.so.2.

试试这个:

$ gcc hello.c -S -masm=intel
$ as -o hello.o hello.s
$ ld -o hello -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o hello.o -lc /usr/lib/crtn.o
$ ./hello
hello, world
$ 

这篇关于直接与LD链接一个C程序失败,未定义的引用`__libc_csu_fini`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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