Get started with C++

Compiling with g++

คำสั่ง g++ คือคำสั่งเรียกคอมไพเลอร์ GNU C++ ซึ่งใช้สำหรับการประมวลผลล่วงหน้า การรวบรวม การประกอบ และการเชื่อมโยงซอร์สโค้ดเพื่อสร้างไฟล์ปฏิบัติการ

Check g++ Version

g++ --version

Let's start using compiler

g++ -c file_name
  • g++ -c file_name ใช้เพื่อคอมไพล์และรวบรวม file_name เท่านั้น และไม่เชื่อมโยง Object code เพื่อสร้าง execute file. มันจะสร้างไฟล์ file_name.o ในไดเร็กทอรีการทำงานปัจจุบัน

g++ file_name -o target_name
  • g++ file_name -o target_name รวบรวมและเชื่อมโยง file_name และสร้าง executable target file ด้วยตั้งชื่อตาม target_name (หรือ a.out ตามค่าเริ่มต้น/default)

ตัวอย่าง

main.cpp
#include <iostream>
using namespace std;

int main() {
   cout << "Hello world!!!" << endl;
   return 0;
}

Header file

Header file จะมาช่วยลดความซับซ้อนและจำนวนบรรทัดของโค้ด นอกจากนี้ยังช่วยให้เราได้รับประโยชน์จากการนำฟังก์ชันที่ประกาศไว้ในไฟล์ส่วนหัวกลับมาใช้ใหม่

".h" เรียกอีกอย่างว่า Header File หมายถึงไฟล์ที่อยู่ส่วนบนของประโยค main() ประกาศเอาไว้ เพื่อเรียกใช้คำสั่งที่ถูกจัดเก็บไว้ใน Header file นั้น ๆ

Ref: https://www.geeksforgeeks.org/compiling-with-g-plus-plus/

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University