How object files add up

December 16, 2009 at 7:30 pm (FOSS, Uncategorized) ()

For those who do not know:

When you compile a single source file using the command: gcc abc.cpp -o out , out is an object file as well as an executable. For a single source file, the object file and executable are both the same.

If you have multiple files, you can write a makefile to generate several object files, each belonging to a certain number of source files. While compiling all these .o object files the makefile helps the object files to come close to each other and form a huddle filling up the vital pieces of a jigsaw puzzle, creating one executable program out of all these .o files.

Advertisements

4 Comments

  1. Debarshi Ray said,

    Even for a single source file an object file and the final executable are not the same. The object file has to be linked to be an executable. It is quite obvious if you compare file sizes when linking with static libraries (or archives). Basically all the undefined symbols in the source have to be resolved to some symbol exported by a library. You can try playing with nm and its friends.

  2. debayan said,

    I knew you would be the first to reply 🙂

    Thanks for the info.

    • Debarshi Ray said,

      There is a very nice (and small & cheap too) book called “Linkers and Loaders” by John R. Levine. You can try to read that if possible. 🙂

  3. anomit said,

    Debarshi is right. When you don’t specify the -c flag, gcc performs the linking too with the intermediate object file and produces the executable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: