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

查看:35
本文介绍了直接与 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 前端
  • 然后运行 ​​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
");
   return 0;
}

我使用以下命令生成汇编代码:

I use the following command to produce the assembly code:

gcc hello.c -S -masm=intel

我告诉 gcc 在用 Intel 语法编译和转储汇编代码后退出.

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/init 似乎是 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.

可能是什么问题?

推荐答案

我发现 另一个帖子,其中包含一个线索:-dynamic-linker/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天全站免登陆