Linking

So far we have discussed the roles of the preprocessor, compiler, and assembler in converting the source code to object code. In the last tutorial, I have mentioned that the object code contains the machine code instructions, but it is not ready for the execution because it has some missing elements. Those missing elements are filled by a software program called linker.

In this tutorial, our focus will be on understanding the role of the linker and the process called linking.


Understanding Linking

As the name suggests, the linker does some linking; i.e., it connects one thing to the another. To be precise, it links the declarations in your code with actual definitions.
Do you remember, in our program, we called the printf() function, which we haven’t defined anywhere? We included the header file stdio.h and I told you that it contains the prototype (or declaration, in other words) of the printf() function, and not the actual definition. Later, we called the function and thought that magically its definition will appear from somewhere and work as expected. In reality, it was not some Harry Potter magic; it was the linker working behind the scenes.

The printf() function is defined in the standard C library (along with other predefined functions). It is the job of the linker to take the object code of our source file (with declarations of predefined functions) and connect all declarations with their definitions. This is the reason I told you that even though the object code contains machine code, it is not yet ready for execution. Resolving the declarations first is important.

After resolving the declarations and other references, linker generates the executable file (in Windows that’s the .exe file) which we can run on our computers.

Note to readers
The linker not only resolves predefined declarations, but it also combines multiple object files into one. In future, you may work on a large project, which may contain multiple C source files. Those files will result into multiple object code files, and in order to make your project work, linker has to combine those object files into one executable file.


How to Generate the Executable File from the Object File?

Let’s generate the executable file from the object file generated in the last tutorial (named example.o) by typing the following command:

gcc example.o

This will generate the executable named a.exe by default.

If at all you want to rename your executable to example.exe (as it matches with the name of the object file), you can type the following command:

gcc example.o -o example.exe

Now, you will observe the file named example.exe in your current working folder (which is C Programs). This is the executable file and it can run on your machine (or computer).

But, if you try running the executable by double clicking it, you may observe that the screen blinks for a second and then goes away. To resolve this, we need to make a couple of changes to our source code.

The original source code (example.c) is as follows:

#include <stdio.h> 
#define N 10 

int main() { 
    printf("Value of N is %d", N); 
    return 0; 
}

and this is the update code:

#include <stdio.h> 
#include <conio.h> 
#define N 10 

int main() { 
    printf("Value of N is %d", N); 
    getch(); 
    return 0; 
}

In the above code, I have included the getch() function in the code which is used to get a character input from the user. Actually, after displaying the output, the window closes too quickly because it was not expecting any input. By using the getch() function, we make the console window stay until the user presses a keyboard button. This little trick stops the console window from disappearing.

Also, at the top, I have included conio.h header file because the getch() function is available in this file.

Kindly make the changes mentioned and save the file.

After saving the file, delete all the old files generated so far (excluding the source file with .c extension).

After deleting the files, we can type the following command in the command prompt to generate an executable from the source code:

gcc example.c -o example.exe

Note to readers
There is no need to type all the commands from the beginning that we have learned so far. The above command is enough to directly generate the executable file from the source file. Of course, behind the scenes, all the stages will be implemented, and corresponding files will be generated but we will only get the executable file.

After successful execution of the command, you will observe the executable file in your current folder. Run the executable by double clicking it, and you will observe the following output:

Value of N is 10

This was expected 😁


Alternate Way to Generate an Executable

Doing all the manual work of writing commands in the command prompt and double clicking the executable file is not needed. We can make Code::Blocks do all the hard work for us.

Click on the Build and Run button, which is available in the top menu bar.

This button will do the same job building (i.e. preprocessing, compiling, assembling, and linking) and running (i.e. executing) the code to produce the final output. So, Code::Blocks gives you the ability to build and run the code with just one click of the button — no need to type all the commands we learned so far 😄

After clicking the button, you will observe the console window (black screen) with the following output:

Value of N is 10

Note to readers:
If you decide to run your program through Code::Blocks, then there is no need to include the getch() function (if you specifically want to use this function to keep the window open) in your program because the Code::Blocks itself will ensure that the console window remains open until the user closes it (via the cross button in the top right corner of the console window).


Summary

  • Linker is a software program whose job is to resolve references in the code.
  • In simple words, it links declarations with the actual definitions and combines multiple object files in the same project.
  • -o is the flag used to generate the executable.
  • getch() function is used to get a character from the user. It is helpful in keeping the command prompt window open.

Conclusion

Build and Run Process

We have completed all the stages of Build and Run successfully. Congratulations 👏

If something is unclear, revisit these tutorials. If you still have doubts, leave a comment below in the comment section. Either I will try my best to answer the queries or someone in the community will help you out.

Happy learning 😁



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.