How can I generate an ELF file with GCC?

会有一股神秘感。 提交于 2019-12-22 06:56:47

问题


I am writing C and C++ code on Linux OS and I am using GCC. After finishing my code, I would like to generate an ELF file. I just can generate "a.out" file and I don't need it. How can I get ELF file ? ELF file occurs as a result of what ? or Is it possible to generate this file with this program ?


回答1:


The compiler (i.e. gcc or g++) will invoke the linker (ld) which produces an ELF executable.

In practice, you will use a builder program (like make) to drive gcc commands. See this answer.

The default output file for gcc is still named a.out (for historical reasons) but is an ELF file. And you really want to ask gcc to output an executable with a more fancy name.

Simple example, you code a single-file hello-world.c program. You can compile it with e.g.

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

(order of arguments to gcc matters a lot!)

and the produced hello-world-bin is an ELF executable. 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.

See also this and that answers.



来源:https://stackoverflow.com/questions/21689261/how-can-i-generate-an-elf-file-with-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!