1

To my understanding, a .o file is the output when you convert a source file to machine code, along with a list of "symbols" which name things like functions and variables.

A .a file is a static library, basically an archive of (unlinked?) object files packed into one file.

A .so file is a shared library, which is a DLL in Windows, which names what sources you want to use, but the .o's themselves are kept externally in the computer until you actually run the program.

An executable is the result of linking a bunch of object files so that all the symbols are defined, and is a runnable file.

My question: For the case where you write a program with all the functions and variables defined and used entirely in one source file, so there is no external linking needed of the .o output, what is the difference between these files? This includes any formatting, size, or performance differences.

Can I run a .o or .a or .so directly? And if I do, is that going to be faster or slower than running an executable?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Zephyr
  • 337
  • 5
  • 23
  • 1
    what about the implicit linking to the standard library? – Sourav Ghosh Nov 24 '16 at 14:54
  • 1
    The .a and .o files are not executable code yet. The linker uses them to produce the final executable image. – Hans Passant Nov 24 '16 at 14:57
  • I guess just assume there's no "includes" in the program. So no but anything you can write normally, like '+' or 'return' is probably necessary to do anything. – Zephyr Nov 24 '16 at 15:03
  • @HansPassant is this also the case when all symbols are resolved in the .a or .o? When I looked up the "linker" I was told that the point is to resolve any undefined symbols in the object files, but if everything is defined in 1 file then all the symbols should be resolved after compilation, so what else is the linker doing? That is the main thrust of my question. – Zephyr Nov 24 '16 at 15:05
  • Erm, it is always the case. A linker cannot produce an executable image if it cannot resolve symbols. Having one source file makes the odds better but you still need to link the CRT and perhaps a library's .a file. – Hans Passant Nov 24 '16 at 15:18
  • So if I do a super simple program like just "return 1;" and compile an object, and run nm on it all it shows is the symbol of "main". So where does the explanation of these other necessary symbols come in? – Zephyr Nov 24 '16 at 18:19

0 Answers0