简体   繁体   English

如何使用GCC生成ELF文件?

[英]How can I generate an ELF file with GCC?

I am writing C and C++ code on Linux OS and I am using GCC. 我正在Linux OS上编写C和C ++代码,我正在使用GCC。 After finishing my code, I would like to generate an ELF file. 完成我的代码后,我想生成一个ELF文件。 I just can generate "a.out" file and I don't need it. 我只能生成“a.out”文件而我不需要它。 How can I get ELF file ? 我怎样才能获得ELF文件? ELF file occurs as a result of what ? ELF文件出现的原因是什么? or Is it possible to generate this file with this program ? 或者是否可以使用此程序生成此文件?

The compiler (ie gcc or g++ ) will invoke the linker ( ld ) which produces an ELF executable. 编译器(即gccg++ )将调用生成ELF可执行文件的链接器( ld )。

In practice, you will use a builder program (like make ) to drive gcc commands. 实际上,您将使用构建器程序(如make )来驱动gcc命令。 See this answer . 看到这个答案

The default output file for gcc is still named a.out (for historical reasons) but is an ELF file. gcc的默认输出文件仍然命名为a.out (由于历史原因),但它是一个ELF文件。 And you really want to ask gcc to output an executable with a more fancy name. 而你真的想让gcc输出一个更具花哨名称的可执行文件。

Simple example, you code a single-file hello-world.c program. 简单的例子,您编写单文件hello-world.c程序。 You can compile it with eg 你可以用eg编译它

 gcc -Wall -g hello-world.c -o hello-world-bin

(order of arguments to gcc matters a lot!) gcc的参数顺序很重要!)

and the produced hello-world-bin is an ELF executable. 生成的hello-world-bin是一个ELF可执行文件。 Check with 检查

 file hello-world-bin

then run it with 然后运行它

 ./hello-world-bin your arguments to it

Later, learn how to use the gdb debugger on it. 稍后,学习如何在其上使用gdb调试器。

See also this and that answers. 另见这个那个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM