CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

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, we are going to discuss the steps to create object files and use them from other code modules.

When we compile a program, the ‘C’ compiler will generate object files (“.o” files. Some compilers will generate “.obj” files.). After that linker will generate a “.out” file (or some compilers will generate “.exe” files.). That means, it is a two steps process; one step is to compile the program and another step is to link the object files and generate the executable file.

Another advantage of an object file is, 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, not allow the users to see what code we have written. The object file is in binary format and it is a compiled code; hence the code is not readable to understand to the user.

Let’s start writing our code module(s).

Generate an Object file

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 we are 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 the “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 mentioned above, we are going to add the “SayHello” function to our module. We will create a “module.c” file and place the “SayeHello” implementation there.

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. We are going to add the 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 the “main” function into our module file. If we compile our code with the 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 the command line.

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

$cc -c module.c

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

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

Linking Object files

Step 4. We have to create code to use the function “SayHello”. We are 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 the “module.h” file into the “sample.c” file. Because the “SayHello” function is declared inside the “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()
{
   SayHello();
}

Step 5. Now we need to compile our “sample.c” file. This is another important step. We need to take special care while compiling the “sample.c” file. If we compile it as a normal compilation; the ‘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 the “SayHello” function is not available in the “sample.c” file. So ‘C’ compiler looks for the “SayHello” function; it throws the error because it didn’t find the implementation of it.

We need to tell to ‘C’ compiler where the “SayHello” implementation exists. We already know that its implementation exists in the “module.o” object file.

Then, how do we instruct the ‘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 the 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 the “a.out” file. Once we run the “a.out” file, we will see the result of the “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

C Programming – How to create and use object files?

One thought on “C Programming – How to create and use object files?

  1. Pingback: Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top