Run C/C++ program on ubuntu( GCC complier)


c/c++ ဆိုတာကေတာ့္ popular အၿဖစ္ဆံုး programming language ေတြထဲကတစ္ခု ပဲၿဖစ္ပါတယ္။c/c++ သာမကပဲ တစ္ၿခားေသာ Programming (Java,PHP,Peral,Scriptin,Ruby..)ေတြလည္း   ubuntu အပါအ၀င္ တစ္ၿခားေသာ distro ေတြမွာ အသံုးၿပဳရတာ အဆင္ေၿပပါတယ္။)

ပထမဦးစြာ c/c++ ကို complie ၿပဳလုပ္ဖို႕အတြက္ လိုအပ္တဲ့ development libraries ေတြကို install ၿပဳလုပ္ဖို႕အတြက္ ေအာက္ေဖာ္ၿပပါ command ကိုအသံုးၿပဳပါ။
sudo apt-get install build-essential
ကၽြန္ေတာ္ကေတာ့္ gcc complier ကိုအသံုးၿပဳမွာ ၿဖစ္ပါတယ္. gcc complier ကို install ၿပဳလုပ္ပါ့မယ္။ေအာက္က command_line ကို အသံုးၿပဳပါ။
 sudo apt-get install gcc

First C program

ကြန္ေတာ္တို႕ install ၿပဳလုပ္ၿပီးၿပီဆိုလွ်င္ ပထမဦးစီး c ပရိုဂမ္တစ္ခုကို စမ္းသပ္ၿပီး complie ၿပဳလုပ္ၾကည့္ပါမယ္။
Desktop ေပၚက ဖိုဒါတစ္ခုထဲမွာ ဖိုင္တစ္ဖိုင္ေဆာက္ၿပီး .c extension နဲ႕ save ၿပဳလုပ္လိုက္ပါ။ၿပီးရင္ေတာ့္ အလြယ္ဆံုး c ပရိုဂမ္တစ္ခုေရးလိုက္ပါ။
#include<stdio.h>
int main()
{
printf(“Welcome to MIET\n”);
return 0;

}

ဖိုင္ထဲကို အေပၚက code ကိုေရးထဲ့ၿပီး နာမည္တစ္ခုခုနဲ႕ save လိုက္ပါ(ဥပမာ miet.c)။

ထို႕ေနာက္ terminal မွ မိမိ C ပရိုဂမ္ file ရွိတဲ့ေနရာသို႕ သြားၿပီး ထို c program file ကို complie ၿပဳလုပ္ဖို႕အတြက္ ေအာက္ေဖာ္ၿပပါ command ကိုအသံုးၿပဳပါမယ္။
gcc miet.c –o test

-o ဆိုတဲ့ option ကေတာ့္ ( specifies the Output File Nmae) ၿဖစ္ၿပီး ကၽြန္ေတာ္တို႕ compile ၿပဳလုပ္လို႕ရတာတဲ့ ဖိုင္ကို နာမည္ေပးတာၿဖစ္ပါတယ္။ အကယ္၍ အဲလိုနာမည္မေပးပဲ gcc miet.c လို႕ complie ၿပဳလုပ္ခဲ့ရင္ default အေနနဲ႕ a ဆိုတဲ့ နာမည္နဲ႕ output ထြက္လာမွာၿဖစ္ပါတယ္။


ယခုC program မွရလာတဲ့ output ဖိုင္ကို run မွာၿဖစ္ပါတယ္။run ဖို႕အတြက္ ./test ၿဖစ္ၿပီး . ဆိုတာကေတာ့္ လက္ရွိတည္ရွိေနတဲ့ file location ၿဖစ္ၿပီး test ကေတာ့္ ရရွိလာတဲ့ output file name ပဲၿဖစ္ပါတယ္။


"Welcome to MIET" ဆိုတဲ့ output ကိုေတြ႕ၿမင္ရမွာၿဖစ္ပါတယ္။

C++ program

ေနာက္တစ္ခု အေနနဲ႕ c++ program ကို run ၾကည့္မွာ ၿဖစ္ပါတယ္။ c++ အတြက္ အသံုးၿပဳရမယ့္ complier command ကေတာ့္ g++ ပဲၿဖစ္ပါတယ္။
C နွင့္ C++ သည္ file extension တူညီမည္မဟုတ္ပဲ C++  သည္ .ccp extension ၿဖစ္ရမည္ ၿဖစ္ပါတယ္။
ယခု C++ file တစ္ခုကို တည္ေဆာက္ၿပီး complie ၿပဳလုပ္ၾကည့္ပါမယ္။


#include<iostream>
using name spacestd;
int main()
{ cout<<”Welcome to MIET”<<endl;
return 0;

}

အေပၚက code ကို ဖိုင္တစ္ခုနဲ႕ ေရးထည့္ၿပီး .cpp extension နဲ႕ save လိုက္ပါ။

ကၽြန္ေတာ္တို႕ C++ program ကို ထံုးစံအတိုင္း file ရွိတဲ့့ location ကို terminal ကေနသြားၿပီး  ေအာက္အတိုင္း complie ၿပဳလုပ္ပါ့မယ္။
g++ miet.cpp –o test
complie ၿပဳလုပ္လို႕ code မွာ error မပါခဲ့ဘူးဆိုလွ်င္ ေအာက္ေဖာ္ၿပပါ test ဖိုင္ေလးရရွိမွာပါ။


၎ကို run ဖို႕အတြက္./test ေအာက္ပါအတိုင္း output ေဖာ္ၿပပါလိမ့္မယ္။ ( . ဆိုတာကေတာ့္ လက္ရွိတည္ရွိေနတဲ့ file location ကိုညြန္းတာၿဖစ္ၿပီး test ကေတာ့္ ခုကရရွိလာတဲ့ output ဖိုင္ပဲၿဖစ္ပါတယ္။)



"Welcome to MIET" ဆိုတဲ့ output ကိုေတြ႕ၿမင္နိဳင္မွာ ၿဖစ္ပါတယ္။ အၿခားေသာ C/C++ program ေတြကိုလည္း ထိုနည္းအတိုင္း complie ၿပဳလုပ္ၿပီး run နိဳင္ပါတယ္။

No comments:

Post a Comment