ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
C++ compiler ဆိုတာ ဘာသာပြန်သူတစ်ယောက်ပါ။ သူက သင့် .cpp file ကို ဖတ်ပြီး language ရဲ့ စည်းမျဉ်းတွေနဲ့ စစ်ကာ file အသစ်တစ်ခု ထုတ်ပေးပါတယ် — သင့်ကွန်ပျူတာရဲ့ ကိုယ်ပိုင် machine code နဲ့ ရေးထားတဲ့ executable program ပါ။ အဲဒီနောက်မှ program ကို run ရတာပါ။ Python နဲ့ မတူတာက အဆင့်နှစ်ဆင့် သီးခြားစီ ရှိပြီး code ပြောင်းတိုင်း နှစ်ဆင့်လုံးကို ပြန်လုပ်ရတာပါ။
ဒါကြောင့် "C++ ကို setup လုပ်တယ်" ဆိုတာ အလုပ်နှစ်ခုပါ — compiler ကို install လုပ်တာနဲ့ terminal က အဲဒါကို ရှာတွေ့အောင် လုပ်တာပါ။ ပထမဆုံး စမ်းသပ်မှု အနည်းငယ်အတွက် online compiler တွေ အဆင်ပြေပေမယ့် local setup ရှိမှ project အစစ်တွေ ဆောက်နိုင်၊ file တွေ သုံးနိုင်ပြီး ကိုယ့် code ထုတ်ပေးတဲ့ warning တွေကို အတိအကျ မြင်ရပါမယ်။
Problem → Why → Solution
ပြဿနာ: tutorial မှာ အလုပ်လုပ်တဲ့ command က သင့်စက်မှာ မလုပ်ဘူး
Tutorial တစ်ခုက compile command ကို ကူးရိုက်လိုက်တော့ အံ့အားသင့်စရာ နှစ်မျိုးထဲက တစ်မျိုး ကြုံရပါတယ်။ တစ်မျိုးက terminal က g++ ဆိုတာ တစ်ခါမှ မကြားဖူးဘူးလို့ ပြောတာပါ။ နောက်တစ်မျိုးကတော့ compiler run သွားပေမယ့် ကိုယ်အတိအကျ ကူးထားတဲ့ code အကြောင်း error တွေ ပြတာပါ။ ပထမတစ်ခုက compiler မရှိသေးတာ ဒါမှမဟုတ် PATH ထဲ မရောက်သေးတာကို ဆိုလိုပါတယ်။ ဒုတိယတစ်ခုကတော့ tutorial က ယူဆထားတာထက် ဟောင်းတဲ့ C++ standard ကို compiler က သုံးနေတာ ဖြစ်တတ်ပါတယ်။
$ g++ hello.cpp -o hello
bash: g++: command not found
$ g++ -std=c++17 hello.cpp -o hello
hello.cpp:6:23: error: 'format' is not a member of 'std'
hello.cpp:6:23: note: 'std::format' is only available from C++20 onwardsဒါတွေ ဘာကြောင့် ဖြစ်တာလဲ
Shell က PATH environment variable ထဲမှာ စာရင်းသွင်းထားတဲ့ folder တွေထဲမှာပဲ command တွေကို ရှာပါတယ်။ ဒါကြောင့် တခြားနေရာမှာ install လုပ်ထားတဲ့ compiler က အဲဒီ folder ကို PATH ထဲ မထည့်မချင်း မမြင်ရပါဘူး။ ပြီးတော့ standard ကို မပြောရင် compiler တိုင်းက ကိုယ့် default standard ကို ရွေးပါတယ် — GCC 16 က C++20 ကို default သုံးပေမယ့် ကျယ်ကျယ်ပြန့်ပြန့် သုံးနေဆဲ GCC version ဟောင်းတွေက C++17 ကို default သုံးပြီး C++17 မှာ std::format မရှိပါဘူး။ -std=c++20 ကို အမြဲ ထည့်ပေးတာက ဒီ ခန့်မှန်းရမှုကို ဖယ်ရှားပေးပါတယ်။
Core concept: source file → compiler → executable
C++ program တစ်ခု build လုပ်တာမှာ အဆင့်သုံးဆင့် ရှိပြီး g++ က command တစ်ခုတည်းနဲ့ အကုန် run ပေးပါတယ်။ Preprocessor က # နဲ့ စတဲ့ line တွေကို ကိုင်တွယ်ပြီး <iostream> လို header တွေကို ကူးထည့်ပါတယ်။ Compiler က syntax နဲ့ type တွေကို စစ်ပြီး code ကို machine code ပါတဲ့ object file အဖြစ် ပြောင်းပါတယ်။ Linker က အဲဒီ object file ကို std::cout နဲ့ std::format ရဲ့ code အစစ်တွေ ရှိတဲ့ C++ standard library နဲ့ ပေါင်းပြီး executable ကို ရေးထုတ်ပါတယ်။
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello ဆိုတဲ့ command က "hello.cpp ကို C++20 အဖြစ် compile လုပ်၊ အဖြစ်များတဲ့ warning တွေနဲ့ အပို warning တွေကို ဖွင့်၊ output ကို hello (Windows မှာ hello.exe) လို့ နာမည်ပေး" လို့ ပြောတာပါ။ -o မပါရင် g++ က Linux နဲ့ macOS မှာ a.out၊ Windows မှာ a.exe လို့ နာမည်ပေးပါတယ်။ ပြီးရင် Unix shell မှာ ./hello၊ PowerShell မှာ .\hello.exe နဲ့ run ပါတယ်။ ./ က current folder ထဲမှာ ရှာဖို့ shell ကို ပြောတာပါ၊ shell နှစ်မျိုးလုံးက default အနေနဲ့ current folder ကို မရှာလို့ပါ။
အဓိက compiler သုံးမျိုး ရှိပါတယ်။ GCC က g++ ကို ပေးပြီး Linux မှာ standard ဖြစ်ပါတယ်။ Clang က clang++ ကို ပေးပြီး macOS မှာ default ဖြစ်ပါတယ်၊ macOS မှာ g++ command က များသောအားဖြင့် Clang ကို ညွှန်းတဲ့ alias ပဲ ဖြစ်ပါတယ်။ Microsoft ရဲ့ MSVC က Visual Studio နဲ့ ပါလာပြီး cl /std:c++20 /W4 /EHsc hello.cpp လို flag အမျိုးအစား ကွဲတာတွေ သုံးပါတယ်။ သုံးမျိုးလုံးက ISO standard တစ်ခုတည်းကို implement လုပ်တာမို့ ဒီ course ထဲက code တွေက C++20 ဖွင့်ထားရင် ဘယ် compiler မှာမဆို compile ဖြစ်ပါတယ်။
Warning တွေက ပထမနေ့ကတည်းက command ထဲမှာ နေရာရသင့်ပါတယ်။ Warning ဆိုတာ code က တရားဝင်ပေမယ့် ကိုယ်ဆိုလိုတာ မဟုတ်လောက်ဘူးလို့ ပြောတာပါ — ဥပမာ value မထည့်ရသေးတဲ့ variable ကို သုံးတာမျိုးပါ။ -Wall -Wextra ပါရင် compiler က ဒီလို ကိစ္စ အများကြီးကို report လုပ်ပါတယ်။ -Werror ကို ထပ်ထည့်ရင် warning တွေ error ဖြစ်သွားလို့ လျစ်လျူရှုလို့ မရတော့ပါဘူး။
- Editor ထဲမှာ hello.cpp ကို ပြင်
- g++ က စစ်ပြီး hello.exe ကို ရေးထုတ်
- ./hello ကို run ပြီး output ဖတ်
- Code ပြောင်းလား? ထပ် build
Source ကို ပြင်ပြီးနောက် executable အဟောင်းကို run မိတာက setup အမှားထဲမှာ အဖြစ်အများဆုံးပါ — အမြဲ အရင် rebuild လုပ်ပါ။
အထွေထွေ ပုံစံ: compile နဲ့ run command များ
# GCC or Clang (Linux, macOS, MinGW on Windows)
g++ -std=c++20 -Wall -Wextra file.cpp -o program
clang++ -std=c++20 -Wall -Wextra file.cpp -o program
./program
# MSVC, inside Developer PowerShell for Visual Studio
cl /std:c++20 /W4 /EHsc file.cpp
.\file.exeGCC နဲ့ Clang က flag တွေ တူပါတယ် — -std= က language version၊ -Wall -Wextra က warning တွေ၊ -o က output နာမည်ပါ။ MSVC ကတော့ / နဲ့ စတဲ့ flag တွေ သုံးပြီး output ကို source file နာမည်အတိုင်း .exe အဖြစ် ပေးပါတယ်။
Example: ကိုယ့် toolchain ကို စစ်တဲ့ hello.cpp
ဒီ program က setup စစ်ဆေးမှု သေးသေးလေး တစ်ခုပါ။ Build အောင်မြင်ရင် compiler၊ C++20 library နဲ့ linker အကုန် အလုပ်လုပ်တယ်ဆိုတာ သိရပြီး program ကိုယ်တိုင်က ဘယ် C++ standard နဲ့ compile ခဲ့လဲ ပြောပြပါတယ်။ g++ -std=c++20 -Wall -Wextra hello.cpp -o hello နဲ့ build လုပ်ပြီး run ကြည့်ပါ။
#include <format>
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
std::cout << std::format("__cplusplus = {}\n", __cplusplus);
std::cout << std::format("Standard year: {}\n", __cplusplus / 100);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 1
#include <format><format> က C++20 header ပါ။ Compiler က C++17 mode မှာ ရှိနေရင် std::format ကို သုံးတဲ့ line တွေမှာ error တက်ပါတယ်။ std::format က GCC 13 နဲ့ Visual Studio 2019 version 16.10 ကမှ စပါလာတာမို့ compiler ဟောင်းတွေမှာ header ကိုယ်တိုင် မရှိနိုင်ပါဘူး။
- Line 5
std::cout << "Hello, C++!\n";ဒီ line ထွက်လာတာနဲ့ compile, link နဲ့ run အဆင့်တွေ အကုန် အလုပ်လုပ်တယ်ဆိုတာ သက်သေပြပါတယ်။
- Line 6
std::format("__cplusplus = {}\n", __cplusplus)__cplusplus က compiler ကြိုသတ်မှတ်ထားတဲ့ macro ဖြစ်ပြီး standard version ကို number အဖြစ် ကိုင်ထားပါတယ် — GCC မှာ C++17 အတွက် 201703၊ C++20 အတွက် 202002၊ C++23 အတွက် 202302 ပါ။ MSVC က /Zc:__cplusplus ကို မထည့်ရင် 199711 ပဲ report လုပ်ပါတယ်။
- Line 7
__cplusplus / 100Integer division က နောက်ဆုံး ဂဏန်းနှစ်လုံးကို ဖယ်ပစ်လို့ 202002 / 100 က 2020 ဖြစ်ပါတယ်။
- Line 8
return 0;Exit status 0 က success ပါ။ Bash မှာ program run ပြီးတာနဲ့ echo $? ရိုက်ရင် 0 ကို မြင်ရပါမယ်။
Hello, C++!
__cplusplus = 202002
Standard year: 2020ကိုယ့်စနစ်ပေါ်မှာ compiler install လုပ်မယ်
| စနစ် | Install လုပ်နည်း | အလုပ်လုပ်လား စစ်နည်း |
|---|---|---|
| Windows (GCC) | MSYS2 ကို install လုပ်ပြီး UCRT64 terminal မှာ pacman -S mingw-w64-ucrt-x86_64-gcc ကို run၊ ပြီးရင် C:\msys64\ucrt64\bin ကို PATH ထဲ ထည့် | Terminal အသစ်မှာ g++ --version |
| Windows (MSVC) | Visual Studio Community ကို "Desktop development with C++" workload နဲ့ install | Developer PowerShell for Visual Studio မှာ cl |
| macOS | xcode-select --install (Apple Clang ကို ထည့်ပေး) | clang++ --version |
| Ubuntu / Debian | sudo apt install build-essential | g++ --version |
| Fedora | sudo dnf install gcc-c++ | g++ --version |
Install ပြီးရင် --version နဲ့ ထွက်လာတဲ့ version number ကို ကြည့်ပါ။ std::format ကို GCC 13 နဲ့ Visual Studio 2019 version 16.10 ကမှ စပါလာတာမို့ Linux distribution တစ်ခုရဲ့ default compiler က ဟောင်းနေရင် version အသစ်တစ်ခု ထပ် install လုပ်ရနိုင်ပါတယ်။ တခြား toolchain တွေအတွက် cppreference ရဲ့ C++20 compiler support table ကို စစ်ကြည့်ပါ။ အောက်က command တွေက နေ့တိုင်း သုံးရမယ့် အလုပ်စဉ်ပါ။
g++ --version # 1. is the compiler on PATH?
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello # 2. build
./hello # 3. run (macOS, Linux, Git Bash)
.\hello.exe # run in Windows PowerShell
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello && ./hello # build, run only if it builtပထမဆုံး compiler error ကို ဖတ်မယ်
GCC နဲ့ Clang error တွေက ပုံစံတူပါတယ် — file:line:column ပြီးရင် error: ဒါမှမဟုတ် warning:၊ ပြီးရင် message ပါ။ နောက်က line တွေက ပြဿနာရှိတဲ့ source line ကို ပြပြီး ^ နဲ့ နေရာကို ညွှန်ပါတယ်။ အောက်က message က std::cout ကို std::cuot လို့ စာလုံးပေါင်းမှားတဲ့အခါ GCC 16 ပြတာပါ — line 4 column 10 မှာ ဖြစ်ပြီး ပြင်ရမယ့် နည်းကိုတောင် အကြံပေးပါတယ်။
hello.cpp: In function 'int main()':
hello.cpp:4:10: error: 'cuot' is not a member of 'std'; did you mean 'cout'?
4 | std::cuot << "Hello, C++!\n";
| ^~~~
| coutပထမ error ကို အရင် ပြင်ပါ
Error တစ်ခုက compiler ကို ရှုပ်ထွေးစေပြီး နောက်က error တွေ အများကြီးကို ဖြစ်စေတတ်ပါတယ်။ အပေါ်ဆုံး error ကို ပြင်၊ ပြန် compile လုပ်ပြီး စာရင်း ဘယ်လောက် တိုသွားလဲ ကြည့်ပါ။
လက်တွေ့ project တွေမှာ
Project အစစ်တွေက C++ ကို ဘယ်လို build လုပ်လဲ
File ရာချီပါတဲ့ project တစ်ခုအတွက် g++ command တွေကို ဘယ်သူမှ လက်နဲ့ မရိုက်ကြပါဘူး။ CMake လို build system တစ်ခုက project ကို ဖော်ပြတဲ့ file တစ်ခု ဖတ်ပြီး compiler command တွေကို ထုတ်ပေးကာ ပြောင်းသွားတဲ့ file တွေကိုပဲ ပြန် compile လုပ်ပါတယ်။ ဒီ lesson ရဲ့ flag တွေက CMakeLists.txt ထဲမှာ တစ်ခါပဲ ရေးရပြီး Linux, macOS, Windows မှာ ညီညီညာညာ သက်ရောက်ပါတယ်။
cmake_minimum_required(VERSION 3.20)
project(hello LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(hello hello.cpp)cmake -S . -B build ပြီးရင် cmake --build build နဲ့ build လုပ်ပါတယ်။ Team တွေက continuous integration server တွေပေါ်မှာ push တိုင်း GCC, Clang နဲ့ MSVC သုံးမျိုးလုံးနဲ့ compile လုပ်ကြပြီး warning တွေကို error အဖြစ် ဆက်ဆံကြပါတယ် — compiler တစ်ခုက လွတ်သွားတဲ့ အမှားကို နောက်တစ်ခုက ဖမ်းမိတတ်လို့ပါ။ Code တိုတိုလေးတွေကို compiler အမျိုးမျိုးနဲ့ နှိုင်းယှဉ်ချင်ရင် developer တွေက Compiler Explorer (godbolt.org) ကို browser ထဲကနေ သုံးကြပါတယ်။
အမှားများ
Setup မှာ အဖြစ်များတဲ့ အမှားတွေ
1. Build fail ဖြစ်ပြီးနောက် build အဟောင်းကို run တာ
Compile fail ဖြစ်ရင် g++ က executable အသစ် မရေးပါဘူး၊ ဒါပေမယ့် အရင် build က hello.exe ကတော့ ကျန်နေပါသေးတယ်။ ./hello ကို run လိုက်ရင် code အဟောင်းက output ကို မြင်ရပြီး ကိုယ့်ပြင်ဆင်မှုက အလုပ်မလုပ်ဘူးလို့ ထင်မိပါတယ်။ g++ ... && ./hello လို့ ရေးပါ — && က build အောင်မြင်မှပဲ run ပါတယ်။
2. C++ ကို g++ အစား gcc နဲ့ compile လုပ်တာ
gcc hello.cpp က .cpp extension ကြောင့် code ကို C++ အဖြစ် compile လုပ်ပေမယ့် C++ standard library ကို link မလုပ်ပါဘူး။ ရလဒ်က linker error တွေပါ — ဥပမာ "undefined reference to `std::cout'" နဲ့ နောက်ဆုံးမှာ "ld returned 1 exit status"။ C++ အတွက် g++ ဒါမှမဟုတ် clang++ ကို သုံးပါ။
3. PATH ပြောင်းပြီး terminal ဟောင်းကို ဆက်သုံးတာ
Terminal တစ်ခုက စဖွင့်ချိန်မှာ PATH ကို ကူးယူထားတာမို့ Windows settings မှာ PATH ပြင်ပြီးသွားလည်း ဖွင့်ထားပြီးသား window တွေက g++ ကို ရှာမတွေ့သေးပါဘူး။ Terminal အသစ် ဖွင့်ပါ (editor ထဲက terminal ဆိုရင် editor ကိုပါ ပြန်ဖွင့်ပါ)၊ ပြီးမှ g++ --version နဲ့ ပြန်စစ်ပါ။
Mini challenge
Mini challenge: ကိုယ့် toolchain ကို သက်သေပြပါ
setup_check.cpp ကို ရေးပြီး -std=c++20 နဲ့ တစ်ခါ၊ -std=c++17 နဲ့ တစ်ခါ compile လုပ်ပါ။ Output နှစ်ခုကို နှိုင်းယှဉ်ပြီး -std flag က ဘာကို ပြောင်းလဲလဲ ကြည့်ပါ။
- __cplusplus ကို standard ဆိုတဲ့ long variable ထဲ သိမ်းရမယ်
- standard >= 202002L ဖြစ်မဖြစ်ကို modern ဆိုတဲ့ bool ထဲ သိမ်းပြီး std::boolalpha နဲ့ true/false အဖြစ် print ထုတ်ရမယ်
- sizeof(int) နဲ့ int တစ်ခု ဘယ်နှ byte သုံးလဲ print ထုတ်ရမယ်
- <format> မသုံးရ — C++17 build ပါ အောင်မြင်ရမယ်
#include <iostream>
int main() {
// TODO: long standard = the value of __cplusplus
// TODO: bool modern = is standard at least 202002L?
// TODO: print a title line
// TODO: print "Standard: <value>"
// TODO: print "C++20 or newer: true/false" (hint: std::boolalpha)
// TODO: print how many bytes an int uses (sizeof(int))
return 0;
}ရလဒ်: C++20 build: Standard: 202002 နဲ့ C++20 or newer: true။ C++17 build: Standard: 201703 နဲ့ C++20 or newer: false။ Build နှစ်ခုလုံးမှာ int က 4 bytes ပါ (desktop platform အများစုမှာ)။
အရိပ်အမြွက် ကြည့်ရန်
std::cout << std::boolalpha << modern; က 1/0 အစား true/false ကို print ထုတ်ပါတယ်။ Build နှစ်ခုကို g++ -std=c++20 -Wall -Wextra setup_check.cpp -o check && ./check နဲ့ run ပြီး နောက်တစ်ခါ -std=c++17 နဲ့ ထပ်လုပ်ပါ။
အဖြေ ကြည့်ရန်
#include <iostream>
int main() {
long standard = __cplusplus;
bool modern = standard >= 202002L;
std::cout << "Setup check\n";
std::cout << "Standard: " << standard << '\n';
std::cout << std::boolalpha << "C++20 or newer: " << modern << '\n';
std::cout << "An int uses " << sizeof(int) << " bytes\n";
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- C++ ကို run ဖို့ အရင် compile လုပ်ရပါတယ် — preprocessor, compiler, linker ပြီးမှ executable ရပါတယ်။
- g++ -std=c++20 -Wall -Wextra file.cpp -o program ကို standard command အဖြစ် သုံးပါ၊ MSVC မှာတော့ cl /std:c++20 /W4 /EHsc ပါ။
- "command not found" က PATH ပြဿနာ၊ file:line:column: error: က compiler ပြဿနာ ဖြစ်ပါတယ်။
- Code ပြင်တိုင်း rebuild လုပ်ပါ၊ g++ ... && ./program က build အောင်မှ run ပါတယ်။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation