How can I generate an ELF file with GCC?

Caner Bacaksız picture Caner Bacaksız · Feb 10, 2014 · Viewed 30.3k times · Source

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 ?

Answer

Basile Starynkevitch picture Basile Starynkevitch · Feb 10, 2014

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.