Compiling C/C++ programs–Object Files

Object files are generated in the process of compilation [Link].They contain a set of machine code instructions (made of lots and lots of 0s and 1s),and computers understand them pretty well.Once you have object files for your C/C++ program ,you are one step away from the final executable file ,which is the whole purpose of going through the hassle of compiling and linking.Linking is actually a simple concept ,it is taking all the object files that your compiler generated and combine them into one final object file .This article offers a soft introduction to Compilers and Linkers.

Object files are the separation line between two big sofware packages involved in building your sofware from the sources : a compiler and a linker.A compiler (like gcc and msvc) goes all the way from the source code , generates object files and it is done. The linker takes over then and it has to generate one and only one final binary executable file.

To generate object files from our source code ,we will be using two software packages : GCC and msvc.We have an article on how to use them to build your Hello World program and we recommend reading if you need a push in the back on how to get started with them. GCC kind of blurs the line between the compiler and the linker ,as you call the same command —gcc both to compile and link ,you use compiler options to make the distinction. On msvc however ,you run separate commands : cl for generating object files ,and explicitly call the LINK command to link your object files .

We are going to compile the same hello world C program we used in this article. We will force msvc and gcc to generate object files without directly linking and explicitly command them to link the files to generate “the” final executable.Also we make sure you can get all this done on Linux and windows. Note:We assume you have your hello world program source files on a directory of choice and that you got gcc,mingw or msvc installed.Again this article covers how to get all this set up for your platform of choice.

Generating object files on Linux using gcc

Open your Linux terminal and change to the directory that contains your source files ,mine is

/home/gakwaya/helloWorld

and I type:

cd /home/gakwaya/helloWorld

List the contents of the directory by typing

ls
linux GCC Dir Contents

linux GCC Dir Contents

 

 

 

 

 

 

 

to generate the object files from those source files ,type:

gcc -c main.c printHello.c

relist the contents of the directory with the ls command and you notice that we have two new files : main.o and printHello.o ,these are our object files generated from the corresponding source files.

linux GCC Dir Contents

linux GCC Dir Contents

 

 

 

 

 

 

 

 

If you open one of these files ,you ll have a chance of seeing what machine code looks like.For example I open main.o on my terminal by typing:

nano main.o

and my object file looks as follows:

Object File Read With Nano

Object File Read With Nano

 

 

 

 

 

 

 

 

 

 

Don’t worry if you can not read it ,it is meant to be read by your computer 🙂 . Now wewant to link those object files.You do this by typing:

gcc main.o printHello.o -o finalBinary

List the contents of your directory and you should see a new file named finalBinary.Run it by typing:

./finalBinary

and you should see hello world printed on your terminal.

linux GCC HelloWorld Printed

linux GCC HelloWorld Printed

Generating object files on Windows using mingw

mingw is a flavor of gcc that has been worked on to make it work on windows ,you can learn more about mingw on their project website.We assume that you got mingw installed on your windows machine.(This article shows how to get it set up) Change to the location of your source files:

cd D:\helloWorld

and command the compiler to generate your object files (without linking) by typing:

gcc -c main.c printHello.c
windows Mingw Compiler Command

windows Mingw Compiler Command

 

 

 

 

 

 

 

 

 

 

 

 

After this your object files are generated .Type dir to show the contents of your build directory as shown below:

windows Mingw Generated Object Files

windows Mingw Generated Object Files

 

 

 

 

 

 

 

 

 

 

 

 

Use one of your favorite text editors (I use notepad++),to open one of those object files and have a look

windows Mingw Object Code Notepad++

windows Mingw Object Code Notepad++

 

 

 

 

 

 

 

 

 

 

 

 

Now we link the files to generate the finalBinary executable, we type:

gcc main.o printHello.o -o finalBinary

If you you list the contents of the directory by typing

dir

You can see the generated binary executable as follows: Type finalBinary.exe (or any other name you specifiled after the -o option of the command) to run your program

finalBinary.exe

and you have hello world printed on your terminal.

windows Mingw Link And Print Hello

windows Mingw Link And Print Hello

 

Generating object files on Windows using msvc

To run commands for the msvc compiler you ve got to have Microsoft visual studio installed on your PC .Open its specialized command prompt(not the standard command prompt) as covered here and change to the directory containing your source code. type

cl /c main.c printHello.c

cl commands msvc to compile and the /c option specifies that it should only generate object files. Typing:

dir

You can see that we have two new object files .Note that the extension of the object files generated by msvc is .obj in contrast to those generated by gcc/mingw which have the .o extension.

windows MSVC Compile And List

windows MSVC Compile And List

To link those files ,just type:

link /out:finalBinary.exe main.obj printHello.obj

Type

dir

To see the generated finalBinary.exe . Type the finalBinary.exe to run your program and you should see hello world printed on your command prompt.

finalBinary.exe
windows MSVC Link And Print Hello

windows MSVC Link And Print Hello

If you have been running commands using both gcc/mingw and msvc you can see that msvc uses separate commands for compiling and linking .This link lists many useful linker options for msvc.

Following is one of our object files opened in Notepad++

windows windows MSVC Object File Notepad++

windows MSVC Object File Notepad++

The Importance of object files

Now that you can generate object files for your source code ,you may be wondering why should someone go through all that trouble when you can just compile without passing through object files as we have here. To understand this ,consider a full blown practical application that is made of thousands of source files and believe me there are such projects.Compiling such a project may take minutes or even hours depending on the size of your project and the speed of your machine.

windows MSVC Object File Windows Explorer

windows MSVC Object File Windows Explorer

 

 

 

 

 

 

 

 

 

In practical programming ,we usually make small changes and want to see the effect on the program. If I make changes to one file and want to see the effect ,waiting minutes to see that isn’t attractive is it? To counter this problem ,the first time the project is compiled ,we generate object files for all the source files and save them in some directory we have specified (Looking into the compiler options for your compiler you can see how to specify the target directory for your object files).

After they are generated ,we link them and generate the executable.When I make a change on one source file and recompile ,I only generate the object file for the file that have changed (It replaces the old one) and leave the other object files alone. I then pass the list of all the object files to the linker and generate a new executable with my changes. With this trick ,what would have for example taken 3 minutes to recompile would take like 6 seconds.

OK I see the improvement in the recompile time but it is tedious to keep track of the files that have changed in my 2000 files project right? Developers saw this problem and come up with a great tool to take care of this for us. The tool is called make .It basically looks at the timestamps of the source file and the object file.If the object file is older than the source file then the object file needs to be regenerated.

There is more to the make tool than just those timestamps ,but what we have seen now is enough to stress how object files help in making the the build process of our software more convenient and effective.Make tools and the makefiles they need to work will be subject of our next tutorial .The purpose was to introduce you to object files and actually get you to generate yours and see them on your own .If you have any questions or suggestions ,just drop me a line.I hope this has been informative to you and I would like to thank you for reading.

Posted in Programming, Tips and tricks, Tutorials and tagged , , , , , .

Daniel Gakwaya loves computer Hardware/Software.He is a Software Engineer at BLIKOON and lead developer of bliboard-The whiteboard system currently marketed by the company.He is known to hack around on any piece of tech that happens to pick his interest. More on his tech endeavors here
Follow him on Twitter