วันนี้ว่าจะทดสอบ c++ บนโน๊ตบุคซะหน่อย เนื่องจากพีซี ที่ใช้คอมไพล์ประจำ เราเอาไปไว้ห้องคลับแล้ว ซึ่งตัวพีซีนั้น ปัจจุบัน ใช้ CentOS 4.0 อยู่นะ แต่ว่า เจ้าโน๊ตบุค ใช้ Ubuntu 5.10
ลองเขียนโปรแกรม Hello World อย่างง่ายขึ้นมา ชื่อไฟล์ hello.cpp นะ
ต่อมา ลองคอมไพล์ ดูนะ
$ g++ hello.cpp -o hello
หรือ อีกแบบ ที่ผมชอบใช้ ในโปรแกรมเล็กๆ
$ make hello
ได้เลยนะ แต่แล้วมันก็เกิด errors ขึ้น เยอะแยะไปหมด
ก็ลองๆ หาข้อมูลในอินเทอร์เน็ตดู วิธีที่ง่ายที่สุด ก็จะทำแบบนี้นะ
$ sudo apt-get install build-essential
จากนั้นก็ใช้งานได้ปกติครับ :0)
แหล่งข้อมูล
https://wiki.ubuntu.com/InstallingCompilers
ลองเขียนโปรแกรม Hello World อย่างง่ายขึ้นมา ชื่อไฟล์ hello.cpp นะ
| #include <iostream> using namespace std; int main() { cout << "Hello, World" << endl; return 0; } |
ต่อมา ลองคอมไพล์ ดูนะ
$ g++ hello.cpp -o hello
หรือ อีกแบบ ที่ผมชอบใช้ ในโปรแกรมเล็กๆ
$ make hello
ได้เลยนะ แต่แล้วมันก็เกิด errors ขึ้น เยอะแยะไปหมด
| In file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++config.h:35, from /usr/include/c++/4.0.2/iostream:43, from hello.cpp:1: /usr/include/c++/4.0.2/i486-linux-gnu/bits/os_defines.h:39:22: error: features.h: No such file or directory In file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++locale.h:41, from /usr/include/c++/4.0.2/iosfwd:45, from /usr/include/c++/4.0.2/ios:43, from /usr/include/c++/4.0.2/ostream:44, from /usr/include/c++/4.0.2/iostream:44, from hello.cpp:1: /usr/include/c++/4.0.2/cstring:51:20: error: string.h: No such file or directory In file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++locale.h:42, from /usr/include/c++/4.0.2/iosfwd:45, from /usr/include/c++/4.0.2/ios:43, from /usr/include/c++/4.0.2/ostream:44, from /usr/include/c++/4.0.2/iostream:44, from hello.cpp:1: ... ... |
ก็ลองๆ หาข้อมูลในอินเทอร์เน็ตดู วิธีที่ง่ายที่สุด ก็จะทำแบบนี้นะ
$ sudo apt-get install build-essential
| Reading package lists... Done Building dependency tree... Done The following extra packages will be installed: libc6-dev linux-kernel-headers Suggested packages: glibc-doc manpages-dev The following NEW packages will be installed: build-essential libc6-dev linux-kernel-headers 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 3835kB of archives. After unpacking 17.9MB of additional disk space will be used. Do you want to continue [Y/n]?y |
จากนั้นก็ใช้งานได้ปกติครับ :0)
แหล่งข้อมูล
https://wiki.ubuntu.com/InstallingCompilers