The C Toolchain — Compiler, Linker, Debugger

পেশাদার C উন্নয়ন পরিবেশ তৈরি করা

~30 min Beginner 6 practice problems Live code

1. Why Understand the Toolchain?

Python hides everything behind one "Run" button. C does not — and that is a blessing. You will meet the preprocessor, compiler, assembler, and linker as real, separate programs. Once you understand each one, every error message will start to make sense.

Python-এ শুধু একটি "Run" বাটনেই সব কাজ লুকানো থাকে। C-তে সেটি নেই — আর এটি আসলে সুবিধাজনক। এখানে preprocessor, compiler, assembler এবং linker — প্রতিটিকে আপনি আলাদা প্রোগ্রাম হিসেবে দেখতে পাবেন। প্রতিটি টুলের কাজ জানলে যেকোনো error message সহজেই বোঝা যায়।

2. Installing a C Compiler

Windows

সবচেয়ে সহজ পথ হলো MSYS2, যা আপনাকে Unix-এর মতো পরিবেশে আসল GCC দেবে।

# 1. Download from https://www.msys2.org
# 2. Open "MSYS2 UCRT64" terminal and run:
pacman -Syu
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-gdb
# 3. Verify
gcc --version
gdb --version
Linux (Ubuntu / Debian)
sudo apt update
sudo apt install build-essential gdb valgrind
gcc --version
macOS
xcode-select --install
clang --version   # macOS uses clang
No setup? Use the cloud
যদি এখনই আপনার মেশিনে GCC ইনস্টল করতে না পারেন, তাহলে উপরের ওয়েবসাইটগুলো ব্যবহার করে সরাসরি ব্রাউজারে C কোড চালাতে পারেন। আমাদের এই কোর্সের প্রতিটি কোড ব্লকেই সরাসরি Run বাটন রয়েছে — তাই শেখার জন্য কোনো সেটআপ লাগবে না।

3. The Compilation Pipeline

When you run gcc hello.c -o hello, four stages happen in order. Each stage can be stopped at and inspected.

Preprocessor hello.c → hello.i Compiler hello.i → hello.s Assembler hello.s → hello.o Linker hello.o → hello Executable ./hello runs -E -S -c (default) Figure 3.1 — C compilation pipeline। নিচে প্রতিটি ধাপে থামার জন্য GCC flag।
Preprocessor header include করে ও ম্যাক্রো expand করে। Compiler C-কে assembly-তে অনুবাদ করে। Assembler assembly থেকে machine code (.o) বানায়। Linker আপনার কোডকে standard library-র সাথে যুক্ত করে final executable তৈরি করে।

আপনি চাইলে যেকোনো ধাপে থামতে পারেন:

gcc -E hello.c -o hello.i   # preprocess only
gcc -S hello.c -o hello.s   # generate assembly
gcc -c hello.c -o hello.o   # assemble to object
gcc    hello.c -o hello     # full pipeline

4. Essential Compiler Flags

FlagPurposeবাংলায়
-o nameset output file nameoutput ফাইলের নাম নির্ধারণ
-Wallenable most warningsগুরুত্বপূর্ণ warning চালু
-Wextraenable extra warningsঅতিরিক্ত warning
-ginclude debug symbolsdebug তথ্য রাখা
-O2 / -O3optimize for speedগতির জন্য optimization
-std=c17use C17 standardC17 standard অনুসরণ
-fsanitize=addresscatch memory bugsমেমরি বাগ ধরা
Daily recommended command gcc -std=c17 -Wall -Wextra -g hello.c -o hello

5. Live Demo — Compile & Run in Your Browser

Below is a complete program you can run right now. Notice the difference -Wall would catch.

pipeline_demo.c
#include <stdio.h>

// Try changing this value and click Run again.
#define N 10

int main(void) {
    long sum = 0;
    for (int i = 1; i <= N; i++) sum += i;
    printf("Sum of 1..%d = %ld\n", N, sum);
    return 0;
}

6. Debugging With GDB

gdb is the GNU Debugger. It lets you pause your program, inspect variables, and step line by line.

gdb হলো GNU Debugger। এটি দিয়ে প্রোগ্রাম চলাকালে থামানো, variable-এর মান দেখা, এবং এক লাইন করে execute করা যায়।
gcc -g program.c -o program
gdb ./program

(gdb) break main      # set breakpoint
(gdb) run             # start program
(gdb) next            # run next line
(gdb) step            # step into function
(gdb) print x         # show variable value
(gdb) backtrace       # call stack
(gdb) continue        # run to next breakpoint
(gdb) quit            # exit

7. Memory Safety — Valgrind & AddressSanitizer

C's most common bug is memory misuse. Two tools will save your life:

  • Valgrind (Linux/macOS): valgrind ./program — finds leaks and invalid reads.
  • AddressSanitizer: compile with -fsanitize=address — catches the same bugs at runtime.
C-র সবচেয়ে সাধারণ বাগ হলো memory-র ভুল ব্যবহার। উপরের দুটি টুল আপনাকে এ ধরনের বাগ দ্রুত ধরতে সাহায্য করবে।

8. Practice Problems

প্রতিটি প্রশ্নের নিচে Show Answer বাটন রয়েছে। উত্তর দেখে কোডটি সরাসরি রান করে যাচাই করতে পারবেন।
  1. Write a minimal hello.c that prints your name. Run it below.
    একটি ছোট hello.c লিখুন যেটি আপনার নাম প্রিন্ট করবে। নিচে চালিয়ে দেখুন।
    ✨ Show Answer
    ans1.c
    #include <stdio.h>
    int main(void) {
        printf("My name is Arif.\n");
        return 0;
    }
  2. A program with a bug — it uses = instead of ==. Run it and observe the output. Then fix it.
    নিচের প্রোগ্রামে ভুলক্রমে ==-এর বদলে = ব্যবহার করা হয়েছে। চালিয়ে output দেখুন, তারপর ভুলটি ঠিক করুন।
    ✨ Show Answer

    Fix: use == for comparison.

    fixed.c
    #include <stdio.h>
    int main(void) {
        int x = 0;
        if (x == 0) printf("x is zero\n");
        else         printf("x is %d\n", x);
        return 0;
    }

    Compile করার সময় -Wall দিলে compiler নিজেই এই ভুল ধরিয়ে দিত।

  3. Write a C program that returns 42 from main. On Linux/macOS, running echo $? should print 42.
    একটি C প্রোগ্রাম লিখুন যা main থেকে 42 return করবে। Linux/macOS-এ echo $? চালালে 42 দেখা যাবে।
    ✨ Show Answer
    exit42.c
    #include <stdio.h>
    int main(void) {
        printf("Exiting with 42\n");
        return 42;
    }
  4. Name the four stages of the C compilation pipeline in order and describe each in one sentence.
    C compilation pipeline-এর চারটি ধাপ ক্রমানুসারে লিখুন এবং প্রতিটির এক লাইনে ব্যাখ্যা দিন।
    ✨ Show Answer
    1. Preprocessor: header include করে এবং macro expand করে।
    2. Compiler: preprocessed C-কে assembly কোডে রূপান্তর করে।
    3. Assembler: assembly থেকে machine code (.o object file) তৈরি করে।
    4. Linker: আপনার object file-কে standard library-র সাথে যুক্ত করে executable বানায়।
  5. Which compiler flag catches the use of an uninitialized variable?
    কোন compiler flag ব্যবহার না-করা variable-এর সমস্যা ধরিয়ে দেয়?
    ✨ Show Answer

    -Wall (তার সাথে -Wextra) সবচেয়ে ভালো কাজ করে। Runtime-এ ধরতে চাইলে -fsanitize=address -fsanitize=undefined ব্যবহার করুন।

  6. Run this program — it allocates 4 bytes then reads 8 bytes. With ASan it would crash loudly. Without, behavior is undefined.
    নিচের প্রোগ্রামটি 4 বাইট allocate করে কিন্তু 8 বাইট পড়ে। AddressSanitizer থাকলে দ্রুত crash হবে; না থাকলে আচরণ অনির্দিষ্ট।
    ✨ Show Answer

    এভাবে বাগ লিখে ফেলা খুবই সাধারণ। সঠিক সংস্করণ:

    fixed_alloc.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
        char *p = malloc(8);       // allocate enough room
        memcpy(p, "ABCLTECH", 8);
        printf("%.8s\n", p);
        free(p);
        return 0;
    }

Glossary (শব্দকোষ)

TermMeaningবাংলায়
ToolchainThe chain of programs that turn source code into an executable.সোর্স কোডকে executable-এ রূপান্তরের জন্য ব্যবহৃত প্রোগ্রামের শৃঙ্খল।
PreprocessorThe first stage — handles #include, #define, macros.প্রথম ধাপ — #include, #define, macro প্রসেস করে।
CompilerTranslates preprocessed C source into assembly / object code.C source-কে assembly বা object code-এ অনুবাদ করে।
AssemblerTurns assembly into machine code object files.Assembly কোডকে machine code object file-এ রূপান্তর করে।
LinkerCombines object files and libraries into one executable.একাধিক object file ও library একসাথে যুক্ত করে executable তৈরি করে।
Object file (.o)Compiled but not yet linked machine code.Compile হয়েছে কিন্তু এখনো link হয়নি — এমন machine code।
ExecutableThe final runnable binary file.চূড়ান্ত চালু-করার-যোগ্য বাইনারি ফাইল।
GCC / ClangThe two most popular C compilers on Linux/macOS/Windows.Linux/macOS/Windows-এ ব্যবহৃত দুটি জনপ্রিয় C compiler।
GDBThe GNU debugger — step through code, inspect variables.GNU debugger — কোড ধাপে ধাপে চালিয়ে variable পরীক্ষা করার টুল।
MakefileA script that tells make how to build your project.make টুলকে প্রজেক্ট কীভাবে build করবে তা জানানোর script।
-Wall / -WextraCompiler flags that turn on extra warnings.অতিরিক্ত warning চালু করার compiler flag।
-gCompiler flag that adds debug information for GDB.GDB-র জন্য debug তথ্য যোগ করার compiler flag।
AddressSanitizer (ASan)Runtime tool that catches memory bugs (out-of-bounds, leaks).Runtime-এ memory bug ধরার টুল (out-of-bounds, leak)।

Summary — Module 03

A C toolchain is four programs in a chain — preprocessor, compiler, assembler, linker. Install GCC/Clang (or use online compilers), always compile with -Wall -Wextra -g, and get comfortable with gdb early. Pair that with AddressSanitizer and you are already ahead of most beginners.

C টুলচেইন = চারটি আলাদা প্রোগ্রামের চেইন। আজ থেকেই -Wall -Wextra -g flag দিয়ে compile করার অভ্যাস গড়ে তুলুন। gdb ও AddressSanitizer আয়ত্ত করলে আপনি বাগ ধরার কাজে অনেক এগিয়ে থাকবেন।

Next Module → Anatomy of main() — hello.c-র প্রতিটি token ব্যাখ্যা।