Compilation and Execution Process of a C Program Using Code::Blocks

🧠 Lecture: Compilation and Execution Process

Duration: 30 minutes
Objective: Understand how to compile and execute a C program using the Code::Blocks IDE, including what happens behind the scenes.


🛠️ 2. Setup and Interface Tour (5–10 minutes)

✅ Steps to Set Up Code::Blocks

  1. Open Code::Blocks
    Launch the IDE and ensure it includes the GCC compiler (such as MinGW).
  2. Create a New Console Application Project
    Go to File > New > Project → Select Console Application → Click Go → Choose C → Follow the wizard to name and create the project.
  3. Explore the Interface
    • Left Panel: Project files (Sources, Headers)
    • Center Panel: Code editor
    • Bottom Panel: Logs and output
    • Top Toolbar: Build, Run, Debug buttons

🔄 3. Compilation Process in Code::Blocks (10–15 minutes)

When you click Build in Code::Blocks, it performs these steps automatically:

  1. Preprocessing: Handles directives like #include and #define
  2. Compilation: Converts source code into assembly language
  3. Assembly: Converts assembly into object code (.o file)
  4. Linking: Links object file with libraries to produce the final executable

📁 Output File Location

YourProjectFolder/
 └── bin/
     └── Debug/
         └── your_program.exe

Tip: Use "Clean" to delete old binaries before rebuilding.


🚀 4. Execution and Output (15–20 minutes)

After building the project:

  • Click Build and Run or press F9.
  • A console window opens showing output from your printf() statements.

🧨 Fix for Auto-Closing Console

Add the following to pause the output window:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Hello from Code::Blocks!\n");
    system("pause");
    return 0;
}

🔍 5. Debugging and Error Handling (20–25 minutes)

🛠️ Example of a Syntax Error

int main() {
    printf("Hello world!")  // ❌ Missing semicolon
    return 0;
}

Code::Blocks will show an error: expected ‘;’ before ‘return’

📊 Using Build Log

  • Go to the Build Messages tab
  • Double-click any error to jump to the line

🐞 Debugging with Breakpoints

  1. Click to the left of the line number to set a breakpoint
  2. Go to Debug > Start/Continue
  3. Use F7 (Step Into) and F8 (Step Over)
  4. Watch variables using the debugger console
int a = 5;
int b = 10;
int result = a + b;

📚 6. Recap and Q&A (25–30 minutes)

✅ Summary

  • Code::Blocks handles all the steps: Preprocessing → Compilation → Assembly → Linking → Execution
  • Executable files are located in the bin/Debug folder
  • Errors and debugging can be done directly in the IDE

🧠 Compilation Flow

main.c 
 ↓ Preprocessor
main.i 
 ↓ Compiler
main.s 
 ↓ Assembler
main.o 
 ↓ Linker
main.exe → Console Output

🗣️ Student Discussion Questions

  • What does "Build" actually do?
  • Where can you find your final executable file?
  • What happens if you press "Run" without building first?

একটি মন্তব্য পোস্ট করুন