The C Toolchain — Compiler, Linker, Debugger
পেশাদার C উন্নয়ন পরিবেশ তৈরি করা
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.
2. Installing a C Compiler
সবচেয়ে সহজ পথ হলো 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
sudo apt update
sudo apt install build-essential gdb valgrind
gcc --version
xcode-select --install
clang --version # macOS uses clang
- OnlineGDB — compile, run, debug in the browser
- Compiler Explorer (godbolt.org) — see the assembly your C produces
- Replit — full cloud IDE
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.
আপনি চাইলে যেকোনো ধাপে থামতে পারেন:
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
| Flag | Purpose | বাংলায় |
|---|---|---|
-o name | set output file name | output ফাইলের নাম নির্ধারণ |
-Wall | enable most warnings | গুরুত্বপূর্ণ warning চালু |
-Wextra | enable extra warnings | অতিরিক্ত warning |
-g | include debug symbols | debug তথ্য রাখা |
-O2 / -O3 | optimize for speed | গতির জন্য optimization |
-std=c17 | use C17 standard | C17 standard অনুসরণ |
-fsanitize=address | catch memory bugs | মেমরি বাগ ধরা |
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.
#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.
8. Practice Problems
-
Write a minimal
hello.cthat 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; } -
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 নিজেই এই ভুল ধরিয়ে দিত। -
Write a C program that returns
42frommain. On Linux/macOS, runningecho $?should print 42.একটি C প্রোগ্রাম লিখুন যাmainথেকে42return করবে। Linux/macOS-এecho $?চালালে 42 দেখা যাবে।✨ Show Answer
exit42.c#include <stdio.h> int main(void) { printf("Exiting with 42\n"); return 42; } -
Name the four stages of the C compilation pipeline in order and describe each in one sentence.C compilation pipeline-এর চারটি ধাপ ক্রমানুসারে লিখুন এবং প্রতিটির এক লাইনে ব্যাখ্যা দিন।
✨ Show Answer
- Preprocessor: header include করে এবং macro expand করে।
- Compiler: preprocessed C-কে assembly কোডে রূপান্তর করে।
- Assembler: assembly থেকে machine code (.o object file) তৈরি করে।
- Linker: আপনার object file-কে standard library-র সাথে যুক্ত করে executable বানায়।
-
Which compiler flag catches the use of an uninitialized variable?কোন compiler flag ব্যবহার না-করা variable-এর সমস্যা ধরিয়ে দেয়?
✨ Show Answer
-Wall(তার সাথে-Wextra) সবচেয়ে ভালো কাজ করে। Runtime-এ ধরতে চাইলে-fsanitize=address -fsanitize=undefinedব্যবহার করুন। -
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 (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
| Toolchain | The chain of programs that turn source code into an executable. | সোর্স কোডকে executable-এ রূপান্তরের জন্য ব্যবহৃত প্রোগ্রামের শৃঙ্খল। |
| Preprocessor | The first stage — handles #include, #define, macros. | প্রথম ধাপ — #include, #define, macro প্রসেস করে। |
| Compiler | Translates preprocessed C source into assembly / object code. | C source-কে assembly বা object code-এ অনুবাদ করে। |
| Assembler | Turns assembly into machine code object files. | Assembly কোডকে machine code object file-এ রূপান্তর করে। |
| Linker | Combines 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। |
| Executable | The final runnable binary file. | চূড়ান্ত চালু-করার-যোগ্য বাইনারি ফাইল। |
| GCC / Clang | The two most popular C compilers on Linux/macOS/Windows. | Linux/macOS/Windows-এ ব্যবহৃত দুটি জনপ্রিয় C compiler। |
| GDB | The GNU debugger — step through code, inspect variables. | GNU debugger — কোড ধাপে ধাপে চালিয়ে variable পরীক্ষা করার টুল। |
| Makefile | A script that tells make how to build your project. | make টুলকে প্রজেক্ট কীভাবে build করবে তা জানানোর script। |
| -Wall / -Wextra | Compiler flags that turn on extra warnings. | অতিরিক্ত warning চালু করার compiler flag। |
| -g | Compiler 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.
-Wall -Wextra -g flag দিয়ে compile করার অভ্যাস গড়ে তুলুন। gdb ও AddressSanitizer আয়ত্ত করলে আপনি বাগ ধরার কাজে অনেক এগিয়ে থাকবেন।