Search code examples
c++cgcccompilationobject-files

What does an object file contain?


During the various stages of compilation in C or C++, I know that an object file gets generated (i.e., any_name.o file). What does this .o file contain? I can't open it since it's a binary file.

Could anybody please help me? Are the contents of the object file mainly dependent on the compiler which we use on Unix?


Solution

  • Object files can contain a bunch of stuff: Basically it's some or all of the list below:

    • Symbol Names
    • Compiled code
    • Constant data, eg. strings
    • Imports - which symbols the compiled code references (gets fixed up by linker)
    • Exports - which symbols the object file makes available to OTHER object files.

    The linker turns a bunch of object files into an executable, by matching up all the imports and exports, and modifying the compiled code so the correct functions get called.