Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.o vs .out in C

Tags:

c

file

output

I compile C code using gcc in a debian system. Normally, I would use gcc file.c -o file.out. But I mistakenly typed gcc file.c -o file.o .

When, running ./file.o it still worked!

What is this .o file, is it same as .out?

like image 241
Kushal Bastakoti Avatar asked Nov 30 '22 13:11

Kushal Bastakoti


1 Answers

By convention, the .o suffix is the object code. GCC and other compilers actually run through several steps when compiling. At a high level, it looks like this**:

  1. pre-processor. This resolves your #define and #include and other #... macros. This step is rarely output to a file - it's almost universally passed directly into the next step.
  2. Object compilation. The lines of code are transformed into machine code and symbol tables. The symbol tables are not yet linked into an executable. For larger programs, this is an intermediate output when you are compiling multiple files which may have dependencies on one another.
  3. Linking. The symbols in the object code are now resolved to the actual memory locations or call points to provide a fully fledged executable.

** yes, this is a simplified view that leaves out optimizations, debugging symbols, stripping, library linkages and a few other steps, but those could also be considered sub steps in the compilation process.

By convention:

  • C files are named .c and .h
  • C++: .cpp and .hpp or .c++ and .h++
  • Object files are named .o.
  • Fortran is .f.
  • Assembly is .as.
  • Static libraries are .a on UNIX and .lib on Windows
  • Dynamic libraries are .so on UNIX and .dll on Windows
  • Executables generally have no extension on UNIX, and .exe on Windows, though there is a default of a.out for linked C and C++ programs that haven't specified an output name.

There is no reason or requirement for the above, except that's what has been done since the '70s and that's what programmers expect.

like image 199
PaulProgrammer Avatar answered Dec 28 '22 02:12

PaulProgrammer