Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธี Edit, Compile และ Execute โปรแกรม C++


ขั้นตอนการสร้างและรันโปรแกรม C++

1. สร้าง Source File

สร้างไฟล์ source.cpp ด้วย text editor:

#include <iostream>
int main() {
    std::cout << "Hello world";
}

2. Compile ด้วย GCC (g++)

$ cd 'C:/Users/Dell/Documents'  # ไปที่โฟลเดอร์ source
$ g++ -o myprogram source.cpp   # compile สร้าง executable myprogram

3. Run โปรแกรม

$ ./myprogram

คำอธิบายคำสั่ง

  • g++ - GNU C++ compiler
  • -o myprogram - ชื่อไฟล์ executable output
  • source.cpp - ไฟล์ source code
  • ./myprogram - รัน executable (Linux/Mac) บน Windows ใช้ myprogram.exe

Compilers ยอดนิยม

  • GCC (g++): https://gcc.gnu.org/
  • Clang: https://clang.llvm.org/get_started.html
  • Visual C++ Community: https://visualstudio.microsoft.com/vs/cplusplus/
  • Online: tutorialspoint.com/compile_cpp_online.php