ดาวน์โหลดบทความดาวน์โหลดบทความ

ถ้าคุณเขียนหรือดาวน์โหลดโค้ดภาษา C ไว้ในคอมพิวเตอร์ Windows ก็ต้อง compile โค้ดให้กลายเป็นโปรแกรมที่จะ run หรือเปิดขึ้นมาได้ซะก่อน บทความสั้นๆ บทความนี้ของวิกิฮาว จะแนะนำขั้นตอนการ compile และ run โปรแกรมภาษา C ใน command prompt ของ Windows ด้วย MinGW ที่เป็น GCC compiler เวอร์ชั่นติดตั้งง่ายให้คุณเอง

  1. How.com.vn ไท: Step 1 ติดตั้ง MinGW ที่เป็น C compiler เวอร์ชั่นพื้นฐาน.
    ถ้ายังไม่ได้ติดตั้ง ให้ติดตั้ง C compiler ลงคอมก่อน เอาไว้ compile โปรแกรมภาษา C การ compile โค้ดภาษา C ก็คือการเปลี่ยนโค้ดเป็นโปรแกรมภาษา C ที่ executable หรือเปิดใช้งานได้ โดย MinGW เป็นหนึ่งในตัวเลือกที่ติดตั้งใน Windows ได้สะดวกที่สุด
    • ดาวน์โหลด MinGW จาก https://sourceforge.net/projects/mingw
    • ดับเบิลคลิกไฟล์ที่ดาวน์โหลดมา แล้วเตรียมติดตั้งไปตามขั้นตอนที่ปรากฏในหน้าจอ
    • พอเจอหน้าต่าง MinGW Installation Manager จะเห็นแพ็คเกจต่างๆ ในกรอบขวาบน
    • ติ๊กช่องข้าง "mingw32-base" และ "mingw-gcc-g++"
    • คลิกเมนู Installation แล้วเลือก Apply Changes
    • คลิก Apply
  2. How.com.vn ไท: Step 2 เพิ่ม path ของ compiler ใน environment variables ของระบบ.
    ขั้นตอนนี้จะทำให้ run ตัว compiler จากใน command prompt ได้ง่ายขึ้น เพราะไม่ต้องมานั่งใส่ path ไปยังโปรแกรม GCC ให้ครบถ้วน
    • กดปุ่ม Windows แล้วพิมพ์ environment จากนั้นคลิก Edit the system environment variables ในผลการค้นหา
    • คลิกปุ่ม Environment Variables…
    • เลือก "Path" ใน "System variables" แล้วคลิก Edit…
    • คลิก New
    • พิมพ์ C:\MinGW\bin แล้วคลิก OK
    • คลิก OK แล้วคลิก OK อีกรอบ
  3. How.com.vn ไท: Step 3 เปิดหน้าต่าง Command Prompt ในฐานะแอดมิน.
    โดยกดปุ่ม Windows พิมพ์ cmd คลิกขวาที่ Command Prompt แล้วเลือก Run as Administrator
    • พอหน้าต่าง command prompt เปิดขึ้นมาแล้ว ให้เช็คว่าติดตั้ง compiler เรียบร้อยดีแล้ว (และตั้งค่า environment variables แล้ว) โดยลอง run คำสั่ง gcc -- version ใน prompt ดู
    • ถ้าเปิดหน้าต่าง command prompt ไว้ ให้ปิดแล้วเปิดอีกหน้าต่างสำหรับ path ที่เพิ่มไป
  4. How.com.vn ไท: Step 4 ใช้คำสั่ง cd เพื่อไปยัง...
    ใช้คำสั่ง cd เพื่อไปยัง directory ที่เซฟโปรแกรมภาษา C ไว้. เช่น ถ้าโปรแกรมที่จะอยู่ใน C:\MyPrograms ก็ให้พิมพ์ cd C:\MyPrograms แล้วกด Enter
  5. How.com.vn ไท: Step 5 ใช้คำสั่ง gcc เพื่อ compile โปรแกรมภาษา C.
    syntax ที่ต้องใช้คือ gcc filename.c -o filename.exe เอาไว้ compile โปรแกรม ทำให้โปรแกรมนั้น executable หรือเปิดใช้งานได้
    • เปลี่ยน filename.c เป็นชื่อไฟล์ของโค้ดภาษา C และเปลี่ยน filename.exe เป็นชื่อของโปรแกรมที่ compile
    • อย่าลืมตั้งชื่อโปรแกรมที่ compiled ให้ลงท้ายด้วย .exe
    • flag -o ใช้ระบุไฟล์ output
  6. How.com.vn ไท: Step 6 พิมพ์ชื่อโปรแกรมใหม่ แล้วกด ↵ Enter.
    เช่น ถ้าสร้างโปรแกรมชื่อ hello.exe ก็ให้พิมพ์ hello หรือ hello.exe เพื่อ run โปรแกรมภาษา C จากใน command prompt
    โฆษณา

เกี่ยวกับวิกิฮาวนี้

How.com.vn ไท: Stan Kats
ร่วมเขียน โดย:
นักเทคโนโลยีมืออาชีพ
บทความนี้ ร่วมเขียน โดย Stan Kats. สแตน แคทส์ เป็นนักเทคโนโลยีมืออาชีพ และเป็น COO และหัวหน้านักเทคโนโลยีของ STG IT Consulting Group ในเวสต์ฮอลลีวูด แคลิฟอร์เนีย สแตนมีการแก้ปัญหาทางเทคโนโลยีแบบครบวงจรให้แก่ธุรกิจต่างๆ ผ่านบริการด้านไอทีที่มีการจัดการ และสำหรับบุคคลผ่านธุรกิจบริการผู้บริโภคชื่อ Stan's Tech Garage สแตนสำเร็จการศึกษาระดับปริญญาตรีศิลปศาสตรบัณฑิตสาขาความสัมพันธ์ระหว่างประเทศจากมหาวิทยาลัยเซาเทิร์นแคลิฟอร์เนีย เขาเริ่มต้นอาชีพโดยทำงานที่ Fortune 500 IT world สแตนก่อตั้งบริษัทเพื่อเสนอความชำนาญระดับองค์กรให้แก่ธุรกิจขนาดเล็กและบุคคลต่างๆ บทความนี้ถูกเข้าชม 1,391 ครั้ง
หมวดหมู่: ซอฟท์แวร์
มีการเข้าถึงหน้านี้ 1,391 ครั้ง

บทความนี้เป็นประโยชน์กับคุณไหม

⚠️ Disclaimer:

Content from Wiki How ไท language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

Notices:
  • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
  • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
  • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
  • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

โฆษณา