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

คอมไพล์โปรแกรม C++ ด้วย GCC

GCC (gcc) คอมไพล์ C++ ได้ แต่ต้อง link libstdc++ เองด้วย flag -lstdc++

ปัญหา: Linker Error

gcc test.cpp
# undefined reference to std::cout, std::endl, etc.

แก้ไข: เพิ่ม -lstdc++

gcc test.cpp -lstdc++
./a.out
# Hello World. This is C++ program

คำแนะนำ

สำหรับโค้ด C++ ควรใช้ g++ โดยตรง จะจัดการ linking และ macros ให้อัตโนมัติ

g++ test.cpp -o test
./test