C Programming: How to create and use object files?

Object files are compiled code modules useful to split large code files into small modules to enable to manage them easily. In this article I am going to explain the steps to create object files and use them from other code modules. When we compile a program, ‘C’ compiler will generate object files (“.o” files. Some compilers will generate “.obj” files.). After that linker will generate “.out” file (or some compilers will generate “.exe” files.). That means, it is a two steps process; one step is compile the program and another step is link the object files and generate the executable file.

Another advantage of an object file is, we no need to distribute the code; just it is enough to distribute the object file(s) to the user(s). This way we can protect our code, to not to allow the users to see what code we have written. Object file is in binary format and it is a compiled code; hence the code is not available to the user.

Lets start writing our code module(s).

Step 1. First we need to create a code for a module file and add the code whatever we want to put in this module file.

  • Here I am going to add a function “SayHello” inside the module file.
  • Create a header file for our code module and place the “SayHello” function declaration here. My header file is “module.h” file.

The header file looks like below:

// module.h
#include <stdio.h>

void SayHello();

Step 2. Now create an implementation file and place the required code in the implementation file. As I mentioned above, I am going to add “SayHello” function to our module. I will create a “module.c” file and place the “SayeHello” implementation here.

The code looks like below:

// module.c
#include "module.h"

void SayHello()
   printf("Hello, World!");

Observe that we are not added main() function into our “module.c” file. I am going to add main() function in another file.

Step 3. Now we need to compile our module file. This is an important step. Remember that we are not added “main” function into our module file. If we compile our code with below command, we will get the following error message:

$ cc module.c
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

So, we need to just compile our module file; without linking. Compiling the file will generate the object file (“.o” file) without generating the “.out” file. “.o” files are not self-executable files; we can’t run them from command line.

We should use the below command to compile our module file.

$cc -c module.c

Above command will compile “module.c” file and generates “module.o” object file. Now you can distribute this object file along with “module.h” file to user(s).

As of now we have learned how to create an object file. Now we will discuss about using object files.

Step 4. We have to create code to use the function “SayHello”. I am going to create a file “sample.c” and include this code inside the file.

  • Add a main() function inside “sample.c” file.
  • Inside main() function call “SayHello” function. Before this, we need to include “module.h” file into “sample.c” file. Because, “SayHello” function is declared inside “module.h” file; we are including this file to avoid any compile time errors or warnings.

The code looks like below:

// sample.c
#include "module.h"

void main()

Step 5. Now we need to compile our “sample.c” file. This is another important step. We need to take special care while compiling “sample.c” file. If we compile it as a normal compilation; ‘C’ compiler will through the below error.

$ cc sample.c
/tmp/ccXYv6Eo.o: In function `main':
sample.c:(.text+0xa): undefined reference to `SayHello'
collect2: ld returned 1 exit status

Because, the implementation of “SayHello” function is not available in “sample.c” file. So ‘C’ compiler looks for “SayHello” function; it throws the error, because it didn’t find the implementation.

We need to tell to ‘C’ compiler where the “SayHello” implementation exists. We already know that its implementation exists in “module.o” object file. Then how do we instruct ‘C’ compiler to look into the object file when we are compiling our program? To resolve the above error we need to mention our object file in command line when we compile our program. The command will be like this:

$cc module.o sample.c

This command will successfully compile and generate “a.out” file. Once we run “a.out” file, we will see the result of “SayHello” function on the screen.

This is the way to create an object file and use the functions in object files from different code modules.

// Malin

Leave a Reply