1

I was reading about ELF files on the net and am stuck in understanding a standard command to generate an ELF file.

Till now I have been running my code using > gcc test.c -o test.o .Thats it!!

One article says:

gcc -c test.c // will generate ELF file test.o

Now -o option is to tell the compiler to create an executable ( which is not ELF)

Another article says:

gcc -c test.c -o test.o // will generate ELF test.o -> here's where I am confused.

-o should always generate Executable.

RootPhoenix
  • 1,626
  • 1
  • 22
  • 40
  • ELF is a format for executable files, so I'm confused by the phrase "an executable ( which is not ELF)" – IMSoP Apr 12 '14 at 17:14
  • "an executable (which is not ELF)" where did you get this idea? – jthill Apr 12 '14 at 17:15
  • Since nobody told me..i guess i took it that way...thanks for clarifying...So should i conclude that "Executable files are subset of all ELF files...there can be many files which are relocatable exe is just one type " ?? – RootPhoenix Apr 13 '14 at 13:31

1 Answers1

4

The option -c tells GCC to generate an object file. This object file is only the compiled code from the source file test.c, not a complete program. To generate a complete program you need to link the object file. Or not use the -c option.

The -o option tells GCC what to name the output file, no matter what kind of output file it is.


So, to generate an executable file from a single source file, the simplest command is

$ gcc test.c

The above command will create an executable named a.out in the current directory. To name the output file something else you use the -o option:

$ gcc test.c -o myprogram

The above commands names the executable program myprogram.

To use the intermediate step with object files you use the -c option, and then use a separate step to link the program, like

$ gcc -c test.c
$ gcc test.o -o myprogram

The above two commands is the same as the single command gcc test.c -o myprogram.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Where can i fit ELF in this? > And this command: gcc -c test.c will it create an ELF > And this command: gcc test.o -o myprogram will it create an ELF – RootPhoenix Apr 13 '14 at 13:57
  • @GNA You don't need to care about what format the executable (or object) file is. On some platforms it will be ELF, on others something else. If all you want to do is to create an executable file, does it really matter what format it's in? – Some programmer dude Apr 13 '14 at 16:59