ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
C code ကို run ဖို့ compiler လိုပါတယ်။ Compiler ဆိုတာ ကိုယ်ရေးထားတဲ့ .c file ကို computer က တိုက်ရိုက် run နိုင်တဲ့ executable program အဖြစ် ပြောင်းပေးတဲ့ tool ပါ။ စလေ့လာတဲ့အချိန်မှာ online editor တစ်ခုခု သုံးတာ အဆင်ပြေပါတယ်။ Project အစစ်တွေအတွက်တော့ GCC ဒါမှမဟုတ် Clang ကို ကိုယ့်စက်ပေါ်မှာ install လုပ်ပါ — ဒါမှ command အတိအကျကို ကိုယ်ထိန်းချုပ်နိုင်ပြီး compiler ထုတ်ပေးတဲ့ message တိုင်းကို ဖတ်နိုင်မှာပါ။
Compiler ကို စာအုပ်တစ်အုပ်လုံးကို ပုံမနှိပ်ခင် ဘာသာပြန်ပေးတဲ့ ဘာသာပြန်သူလို့ မြင်ကြည့်ပါ။ ဘာသာပြန်ပြီးသား (executable) ရပြီဆိုတာနဲ့ စာဖတ်သူတွေက ဘာသာပြန်သူကို မလိုတော့ပါဘူး — source ကို မပြင်မချင်း program ကို compile ပြန်မလုပ်ဘဲ အကြိမ်ကြိမ် run လို့ ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: Run ခလုတ်က ဘယ်အဆင့် ပျက်လဲဆိုတာကို ဖုံးထားတယ်
ကိုယ့် program က greet() ဆိုတဲ့ helper function တစ်ခုကို ခေါ်တယ်ဆိုပါစို့။ File ရဲ့ အပေါ်မှာ declare လုပ်ထားပေမယ့် body ရေးဖို့ မေ့သွားပါတယ်။ Run နှိပ်လိုက်တော့ "collect2.exe: error: ld returned 1 exit status" နဲ့ အဆုံးသတ်တဲ့ စာသားအရှည်ကြီး ထွက်လာပါတယ် (path နဲ့ file နာမည်တွေက system ပေါ်မူတည်ပြီး ကွဲပါတယ်)။ ကိုယ့် file ထဲက line number ကို အလွယ်တကူ မတွေ့ရသလို greet() ကို ခေါ်တဲ့ line ကို ပြင်လည်း ဘာမှ မပြောင်းလဲပါဘူး။
Run ခလုတ်ကိုပဲ နှိပ်နေခဲ့ရင် ဒါက စာလုံးပေါင်းမှားတာလား၊ setup ပြဿနာလား၊ bug လား ခွဲပြောဖို့ ခက်ပါတယ်။ Build တစ်ခုမှာ သီးခြားအဆင့်တွေ ရှိတယ်ဆိုတာ သိသွားတာနဲ့ message ကို ဖတ်လို့ရလာပါတယ် — ld ဆိုတာ linker ဖြစ်ပြီး "undefined reference to `greet'" ဆိုတာ linker က ခေါ်တာကို တွေ့ပေမယ့် အဲဒီ function ရဲ့ code ကို မတွေ့ဘူးလို့ ပြောတာပါ။
#include <stdio.h>
void greet(void); /* declared: the compiler is satisfied */
int main(void) {
greet(); /* compiles fine, but no body exists anywhere */
return 0;
}ဘာကြောင့် compiler က ဒါကို မဖမ်းတာလဲ
Compile လုပ်တာနဲ့ link လုပ်တာက tool မတူတဲ့ သီးခြားအဆင့် နှစ်ခုပါ။ Compiler က greet() ကို declare လုပ်ထားသလားဆိုတာပဲ စစ်တာမို့ ခေါ်တဲ့နေရာမှာ အပေါက်တစ်ခု ချန်ထားပြီး machine code ကို ကျေကျေနပ်နပ် ထုတ်ပေးပါတယ်။ အဲဒီ အပေါက်တွေကို ကိုယ့် file တွေ ဒါမှမဟုတ် standard library ထဲက တကယ့် code နဲ့ ဖြည့်ပေးတာက linker ရဲ့ အလုပ်ဖြစ်လို့ body ပျောက်နေတာကို သတိထားမိတာ linker ပါ။ Compiler message တွေက file:line:column နဲ့ စပြီး linker message တွေကတော့ ld ဆီကလာပြီး line အစား symbol နာမည်ကို ပြောပါတယ်။
Core concept: toolchain နဲ့ gcc command
C toolchain ဆိုတာ အတူတကွ အလုပ်လုပ်တဲ့ program အစုတစ်ခုပါ — preprocessor, compiler, assembler, linker နဲ့ C standard library တို့ဖြစ်ပြီး GDB ဒါမှမဟုတ် LLDB လို debugger တစ်ခုလည်း များသောအားဖြင့် ပါလာပါတယ်။ သူတို့ကို တစ်ခုချင်း ခေါ်ရတာ ရှားပါတယ်။ gcc (ဒါမှမဟုတ် clang) command က driver တစ်ခုပါ — tool တစ်ခုချင်းကို အစဉ်လိုက် run ပြီး ရလဒ်ကို နောက်တစ်ခုဆီ လက်ဆင့်ကမ်းပေးလို့ command တစ်ကြောင်းတည်းနဲ့ hello.c ကို ပြီးပြည့်စုံတဲ့ executable အဖြစ် ပြောင်းနိုင်ပါတယ်။
Driver ဘာလုပ်မလဲဆိုတာကို option တွေက ထိန်းပါတယ်။ -std=c17 က C17 စည်းမျဉ်းတွေကို ရွေးပေးလို့ project ကို build လုပ်သူတိုင်း language version တစ်ခုတည်းကို ရပါတယ်။ ချန်ထားခဲ့ရင်တော့ ကိုယ့် compiler version ရဲ့ default mode ကို ရပါလိမ့်မယ်။ -Wall နဲ့ -Wextra က အသုံးဝင်ဆုံး warning တွေကို ဖွင့်ပေးပါတယ်။ -o hello က output file ကို နာမည်ပေးပါတယ် — မပါရင် GCC က Linux နဲ့ macOS မှာ a.out၊ Windows မှာ a.exe ကို ရေးပါတယ်။ -g က debugging information ထည့်ပေးပြီး -c ကတော့ compile ပြီးတာနဲ့ ရပ်လိုက်လို့ executable အစား object file (hello.o) ကို ရပါတယ်။
| အဆင့် | Command | ရလဒ် |
|---|---|---|
| Compile သက်သက် | gcc -std=c17 -Wall -Wextra -c hello.c | hello.o (object file) |
| Link | gcc hello.o -o hello | hello (Windows မှာ hello.exe) |
| နှစ်ခုလုံး တစ်ခါတည်း | gcc -std=c17 -Wall -Wextra hello.c -o hello | Executable ပဲ ရပြီး .o ကို မသိမ်းထားပါ |
| Run | ./hello | Program ရဲ့ output နဲ့ exit status |
Program run တဲ့အခါ main ရဲ့ return value က process ရဲ့ exit status ဖြစ်လာပါတယ်။ 0 ဒါမှမဟုတ် <stdlib.h> ထဲက EXIT_SUCCESS က success ကို ဆိုလိုပြီး EXIT_FAILURE လို တခြား value တွေကတော့ shell ဒါမှမဟုတ် build script ကို တစ်ခုခု မှားသွားကြောင်း ပြောပါတယ်။ C99 ကစပြီး return statement မပါဘဲ main ရဲ့ ပိတ်တဲ့ brace ကို ရောက်သွားရင်လည်း 0 ကို return ပြန်ပါတယ်၊ ဒါပေမယ့် return 0; ကို ရေးထားတာက ရည်ရွယ်ချက်ကို မြင်သာစေပါတယ်။ Bash လို shell မှာ program ပြီးပြီးချင်း echo $? နဲ့ status ကို ကြည့်နိုင်ပြီး PowerShell မှာတော့ $LASTEXITCODE ထဲမှာ ရှိပါတယ်။
- gcc က hello.c နဲ့ option တွေ ဖတ်
- Compiler က object file ရေး
- Linker က libc ပေါင်းပြီး hello ရေး
- Shell က run ပြီး exit status ဖတ်
ပြေးပွဲ ရပ်သွားရင် တုတ်ချပစ်ခဲ့တဲ့ အပြေးသမားက သူ့ကိုယ်သူ နာမည်ပြောပါတယ် — file:line error တွေက compiler ဆီက၊ ld error တွေက linker ဆီက လာတာပါ။
အထွေထွေ ပုံစံ: build နဲ့ run command များ
# compile and link in one step
gcc -std=c17 -Wall -Wextra source.c -o program
# or in two steps: compile to an object file, then link
gcc -std=c17 -Wall -Wextra -c source.c
gcc source.o -o program
# run it (on Windows the file is program.exe)
./programပထမပုံစံက file တစ်ခုတည်း program တွေအတွက် အသုံးအများဆုံးပါ။ ဒုတိယပုံစံက driver လုပ်နေတဲ့ အဆင့်နှစ်ခုကို မြင်သာအောင် ခွဲပြတာဖြစ်ပြီး .c file တစ်ခုကို ပြင်တိုင်း အဲဒီ file တစ်ခုတည်းကိုပဲ ပြန် compile လုပ်ချင်တဲ့ project ကြီးတွေမှာ အသုံးဝင်ပါတယ်။
Example: ကိုယ့် C version ကို report လုပ်တဲ့ hello.c
ဒါက ထုံးစံ ပထမဆုံး program ပါ၊ ထပ်ဖြည့်ထားတာ တစ်ခုပဲ ရှိပါတယ် — compiler က ကြိုသတ်မှတ်ပေးထားတဲ့ __STDC_VERSION__ macro ကို သုံးပြီး ဘယ် C standard အတွက် compile လုပ်ခဲ့လဲ print ထုတ်ပါတယ်။ ကိုယ့်စက်မှာ အောက်က command တွေနဲ့ compile လုပ်ပြီး -std option မတူတာတွေနဲ့ ဒုတိယ line ကို နှိုင်းယှဉ်ကြည့်ပါ။
gcc -std=c17 -Wall -Wextra hello.c -o hello
./hello
echo $? # exit status of the last command: 0// hello.c
#include <stdio.h>
int main(void) {
printf("Hello C learner!\n");
printf("Compiled as C version %ld\n", __STDC_VERSION__);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 1
// hello.cFile ကို နာမည်ပြောပြတဲ့ comment ပါ။ Compiler က လျစ်လျူရှုပေမယ့် file နာမည်က ကိုယ့်အတွက် အရေးကြီးပါတယ် — source file က .c နဲ့ ဆုံးရပါမယ်။
- Line 2
#include <stdio.h>Standard I/O header ကနေ printf ရဲ့ declaration ကို ယူလာပါတယ်။ printf ရဲ့ တကယ့် code ကတော့ C library ထဲမှာ ရှိပြီး linker က ထည့်ပေးတာပါ။
- Line 4
int main(void) {Program ဒီကနေ စ run ပါတယ်။ (void) လို့ ရေးတာက main မှာ argument မရှိကြောင်း ပြောတာဖြစ်ပြီး C standard တိုင်းမှာ တစ်ပုံစံတည်း အလုပ်လုပ်ပါတယ်။
- Line 6
printf("Compiled as C version %ld\n", __STDC_VERSION__);__STDC_VERSION__ က compiler က ကိုယ့်အတွက် define လုပ်ပေးထားတဲ့ number ပါ — -std=c17 အောက်မှာ 201710၊ -std=c11 အောက်မှာ 201112 ဖြစ်ပြီး တခြား mode တွေမှာ တခြား value တွေ ထွက်ပါတယ်။ သူ့ type က long ဖြစ်လို့ format က %ld ပါ။
- Line 7
return 0;Exit status ဖြစ်လာပါတယ်။ Program run ပြီးချင်း echo $? လို့ ရိုက်ရင် 0 ကို print ထုတ်ပါတယ်။
Hello C learner!
Compiled as C version 201710Compiler တစ်ခု install လုပ်ခြင်း
| System | ရနိုင်တဲ့ နည်းလမ်း |
|---|---|
| Debian / Ubuntu Linux | sudo apt install build-essential (gcc နဲ့ make ပါပါတယ်) |
| macOS | xcode-select --install က Apple ရဲ့ Clang ကို install လုပ်ပေးပြီး gcc command ကလည်း Clang ကိုပဲ run ပါတယ် |
| Windows | MSYS2 ဒါမှမဟုတ် w64devkit ကနေ MinGW-w64 GCC၊ ဒါမှမဟုတ် WSL ထဲမှာ Linux toolchain |
Install ပြီးရင် terminal အသစ်တစ်ခု ဖွင့်ပြီး gcc --version ကို run ကြည့်ပါ။ Version line တစ်ကြောင်း ထွက်လာရင် PATH ပေါ်မှာ compiler ရှိနေပါပြီ။ "command not found" လို message ထွက်ရင် compiler ရဲ့ bin folder ကို PATH ထဲ မထည့်ရသေးတာ ဖြစ်တတ်ပါတယ်။ Microsoft ရဲ့ MSVC compiler က option တွေကို တခြားပုံစံနဲ့ ရေးပါတယ်၊ ဒီ course ကတော့ Clang ကလည်း လက်ခံတဲ့ GCC-style command တွေကို သုံးပါတယ်။
Compiler message တစ်ကြောင်းကို ဖတ်ခြင်း
Anatomy of a GCC diagnostic
---------------------------
semi.c:4:26: error: expected ';' before 'return'
| | | | |
| | | | +-- what the compiler expected to see
| | | +--------- severity: error stops the build, warning does not
| | +-------------- column
| +---------------- line
+--------------------- fileဒီ message က line 4 မှာ semicolon မေ့ခဲ့တဲ့ file ကနေ လာတာပါ။ Error က build ကို ရပ်လိုက်ပြီး executable အသစ် မထွက်ပါဘူး။ Warning ကတော့ build ကို ဆက်သွားခွင့်ပေးပေမယ့် program မှာ bug ရှိနိုင်ကြောင်း ပြောတာပါ။ Message ရဲ့ ပထမဆုံး စကားလုံးတွေက file နာမည်နဲ့ line number ဆိုရင် compiler ဆီက ဖြစ်ပြီး ld ဒါမှမဟုတ် collect2 ပါရင် linker ဆီက ဖြစ်ပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
File ငါးဆယ်ပါတဲ့ project တစ်ခုအတွက် gcc command တွေကို လက်နဲ့ ဘယ်သူမှ မရိုက်ကြပါဘူး။ make နဲ့ CMake လို build tool တွေက command တွေကို သိမ်းထားပြီး ပြောင်းလဲသွားတာကိုပဲ ပြန် run ပါတယ်။ Compile နဲ့ link က သီးခြားဖြစ်နေလို့ .c file တစ်ခု ပြင်ရင် အဲဒီ file တစ်ခုကိုပဲ .o အဖြစ် ပြန် compile လုပ်ပြီး ပြန် link လုပ်ရုံပါပဲ၊ အကုန်လုံးကို ပြန်တည်ဆောက်စရာ မလိုပါဘူး။ နောက်ကွယ်မှာတော့ ဒီ lesson မှာ သင်ခဲ့တဲ့ compiler driver နဲ့ flag တွေကိုပဲ ခေါ်နေတာပါ။
Team တွေက language mode နဲ့ warning တွေကို build configuration ထဲမှာ ပုံသေ သတ်မှတ်ထားကြပါတယ် — ဥပမာ CI pipeline တစ်ခုမှာ -std=c17 -Wall -Wextra -Werror၊ -Werror က warning တိုင်းကို build failure အဖြစ် ပြောင်းလို့ warning အသစ်တွေ လျှို့ဝှက်ဝင်မလာနိုင်ပါဘူး။ CI build ကျသွားရင် log ထဲမှာ ဒီ lesson က compiler နဲ့ linker message တွေ အတိအကျ ပေါ်လာပြီး "ဒါ linker error ပါ၊ link line မှာ library တစ်ခု ကျန်နေတယ်" လို့ ပြောနိုင်တာက ခန့်မှန်းရတာတွေကို အများကြီး သက်သာစေပါတယ်။ GDB လို debugger တွေကလည်း machine code ကို ကိုယ့် source line တွေဆီ ပြန်ချိတ်ဖို့ -g option လိုပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Executable အဟောင်းကို run နေတာ
hello.c ထဲက bug ကို ပြင်ပြီး ./hello run လိုက်တော့ output အဟောင်းပဲ ထွက်လာပါတယ်။ Executable က သီးခြား file တစ်ခုဖြစ်လို့ compile ပြန်မလုပ်မချင်း မပြောင်းပါဘူး။ Compile ပြီးရင် run ဆိုတာကို အလေ့အထ တစ်ခုတည်း ဖြစ်အောင် လုပ်ပါ — ဥပမာ gcc ... -o hello && ./hello၊ && က build အောင်မြင်မှပဲ program ကို run ပါတယ်။ Build မှာ error တက်ခဲ့ရင် file အဟောင်းက ဆက်ရှိနေတာကိုလည်း သတိပြုပါ။
2. -o မေ့ပြီး file မှားကို run တာ
-o မပါရင် GCC က a.out (Windows မှာ a.exe) ကို ရေးပါတယ်။ hello အဟောင်းတစ်ခု ရှိနေသေးရင် ./hello က အဲဒီ ဟောင်းနေတဲ့ file ကို run ပြီး build အသစ်ကတော့ a.out ထဲမှာ ထိုင်နေပါလိမ့်မယ်။
3. Definition အစား ခေါ်တဲ့နေရာကို ပြင်နေတာ
"undefined reference to `greet'" မှာ ခေါ်တာက မှန်ပါတယ်၊ linker က body ကို ရှာမတွေ့တာပါ။ ပျောက်နေတဲ့ definition၊ definition ထဲမှာ နာမည် စာလုံးပေါင်း မှားတာ ဒါမှမဟုတ် command line ပေါ်မှာ ထည့်ဖို့ မေ့ခဲ့တဲ့ .c file ကို ရှာပါ — ဥပမာ greet က greet.c ထဲမှာ ရှိနေပေမယ့် gcc main.c -o app လို့ပဲ ရိုက်ခဲ့တာမျိုးပါ။
4. Header ကို ချန်ထားခဲ့တာ
#include <stdio.h> မပါဘဲ printf ခေါ်ရင် GCC ရဲ့ မကြာသေးခင် version တွေက "implicit declaration of function 'printf'" ဆိုတဲ့ error ပေးပြီး '<stdio.h>' ကို include လုပ်ဖို့ အကြံပြုပါတယ်။ Compiler အဟောင်းတွေကတော့ warning သက်သက်ပဲ ပေးခဲ့လို့ tutorial အဟောင်းတွေမှာ ဒီ line ကျန်နေတာ တွေ့ရနိုင်ပါတယ်။
Mini challenge
Mini challenge: build မအောင်တဲ့ file ကို ပြင်ပါ
အောက်က greet.c က build မအောင်ပါဘူး။ ပြဿနာ သုံးခုကို တစ်ခုချင်း ပြင်ပြီး ပြင်ပြီးတိုင်း ပြန် compile လုပ်ကာ message က compiler ဆီကလား linker ဆီကလား မှတ်ထားပါ။
- printf ကို declare လုပ်ထားတဲ့ header ကို ထည့်ရမယ်
- Build က ပထမဆုံး report လုပ်တဲ့ compiler error ကို ပြင်ရမယ်
- Link အဆင့် အောင်အောင် print_banner() ရဲ့ ပျောက်နေတဲ့ definition ကို ရေးရမယ်
- gcc -std=c17 -Wall -Wextra နဲ့ build လုပ်ပြီး warning သုည ဖြစ်ရမယ်
// greet.c: this file does not build yet. Fix it one error at a time.
// TODO 1: printf needs its header; add the right #include line here
void print_banner(void); // declared here, but never defined
int main(void) {
print_banner();
printf("Build log: compile ok, link ok\n") // TODO 2: compiler error
return 0;
}
// TODO 3: define print_banner() so the linker can find it.
// It should print "=== greet v1 ===" on its own line.ရလဒ်: ပြဿနာ သုံးခုလုံး ပြင်ပြီးရင် warning မရှိဘဲ build အောင်ပြီး ဒီ line နှစ်ကြောင်း ထွက်လာရပါမယ်: === greet v1 === Build log: compile ok, link ok
အရိပ်အမြွက် ကြည့်ရန်
အားလုံးပြီးမှ မဟုတ်ဘဲ ပြင်ပြီးတိုင်း build လုပ်ပါ။ Definition တစ်ခုက declaration ရဲ့ ပထမ line ကို ပြန်ရေးပြီး ; ကို { } နဲ့ အစားထိုးကာ statement တွေကို အထဲမှာ ထည့်ရတာပါ။ နောက်ဆုံး message က ld ဆီကလာပြီး print_banner ကို နာမည်တပ်ပြောပါလိမ့်မယ်။
အဖြေ ကြည့်ရန်
// greet.c
#include <stdio.h>
void print_banner(void);
int main(void) {
print_banner();
printf("Build log: compile ok, link ok\n");
return 0;
}
void print_banner(void) {
printf("=== greet v1 ===\n");
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- gcc လို compiler driver က preprocessor, compiler နဲ့ linker ကို ကိုယ့်အစား run ပေးပြီး -c က compile ပြီးတာနဲ့ ရပ်ပါတယ်။
- Language mode နဲ့ warning တွေ ရှင်းရှင်းလင်းလင်း ဖြစ်အောင် gcc -std=c17 -Wall -Wextra file.c -o name နဲ့ build လုပ်ပါ။
- file:line:column message တွေက compiler ဆီက ဖြစ်ပြီး "undefined reference" က linker ဆီက လာတာပါ။
- main ရဲ့ return value က exit status ဖြစ်ပြီး 0 ဒါမှမဟုတ် EXIT_SUCCESS က success ကို ဆိုလိုပါတယ်။
- Source ပြင်တိုင်း compile ပြန်လုပ်ပါ၊ executable က သူ့ဘာသာ update မဖြစ်ပါဘူး။
ISO C Working Group (WG14) — ISO/IEC JTC1/SC22/WG14