Linking

In this tutorial, we will delve into the workings of the linker and gain an understanding of the linking process.


The Linking

The linker plays a crucial role in connecting declarations with their actual definitions.

Previously, we generated the object file. Now, we can create the executable file by entering the following command:

gcc add.o

Alternatively, we can also type the following command:

gcc add.o -o add.exe

Once you’ve entered the command mentioned above (or the previous command), you will notice an executable file named add.exe (specific to Windows machines) in the same “C Programs” folder. However, you cannot run this file by simply double-clicking on it. You need to execute it through the command prompt by typing “add.exe” or “add”. Give it a try! Did you see the console window displaying the expected output? If yes, that’s great! 👍


How to run the executable by double-clicking it?

To enable running the executable by double-clicking it, we need to make the following changes in our source code:

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

int main() 
{
    printf(“The value of N is %d”, N);
    getch();
    return 0;
}

In the updated code, two extra lines have been added for a specific purpose. Let’s understand their role:

  • Line #2: #include <conio.h> is added to include the header file conio.h. This header file provides the declaration of the getch() function, which we want to use in our program. This function allows us to capture a single character input from the user. By including this header file, we ensure that the compiler knows about the getch() function and can properly compile our code.
  • Line #8: The call to getch() function is added. This function waits for the user to enter a character. Once the user types a character, the console window will disappear. This allows us to keep the console window open until we provide input, giving us the ability to run the executable by double-clicking it.

By using getch() and waiting for user input, we can prevent the console window from closing immediately after the program execution completes. This way, we can observe the output and keep the console window open until we’re ready to close it by providing input.

Once you have made the necessary changes to your code, it’s important to delete the old executable file (the one with the .exe extension) from your C Programs folder. This ensures that you’re working with the updated version of your code.

After deleting the old executable, you can proceed to build your code again using Code::Blocks. Open your source code file in Code::Blocks and go to the Build menu. From there, click on Build or use the shortcut CTRL + F9. This will compile your code and generate a new executable file named add.exe in the same folder.

Now, you can double-click the new add.exe file, and it will open the console window (the black screen) displaying the desired output.

Alternatively, you have the option to directly run your code within the Code::Blocks IDE. You can do this by clicking on the Build and Run button or by using the shortcut CTRL + F10. This will compile your code and automatically execute it within the IDE, displaying the output in the console window.

Both methods, running the executable directly or using the Code::Blocks IDE, allow you to see the output of your program. Choose the method that suits your preference.

It’s important to note that if you have already built your code by clicking the “Build” button in Code::Blocks and you haven’t made any changes to your code since then, you don’t need to click the “Build and Run” button to execute your code. In this case, you can simply click on the “Run” button.

The “Run” button is specifically used to execute your code without the need for rebuilding it. It assumes that the previously built executable file is up to date and can be directly executed. So, if you haven’t made any changes to your code and have already built it, you can just click on the “Run” button to execute your code. The output will be displayed in the console window.


Conclusion

We have now learned about the process of executing a program in detail. However, for the sake of simplicity and convenience, we will follow a simple 3-step process to execute all the programs we write in the future:

  1. Write a C program according to the problem or task at hand.
  2. Save the program to ensure that our changes are preserved.
  3. Build and Run the C program using the Code::Blocks IDE. This step will compile our code, check for any errors, and execute the program. We can easily identify and address any errors through the error logs provided by Code::Blocks.

Using Code::Blocks IDE offers us the advantage of checking for errors in our code without the need to open any other application. This simplifies the debugging process and allows us to quickly identify and correct any issues in our programs.



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.