ทดลอง compile ภาษา C ด้วย gtk
ผมทดลองภาษา C เขียนโปรแกรมด้วย GTK
ตามตัวอย่างในเวป http://www.gtk.org/tutorial/c39.html
ดังนี้
#include
int main( int argc,
char *argv[] )
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
เก็บไว้ในไฟล์ชื่อ hello.c แล้วตอน compile ด้วยคำสั่ง
gcc hello.c -o hello `pkg-config --cflags --libs gtk+-2.0`
พบปัญหาดังนี้
bash: pkg-config: command not found
hello.c:1:21: error: gtk/gtk.h: No such file or directory
hello.c: In function ‘main’:
hello.c:6: error: ‘GtkWidget’ undeclared (first use in this function)
hello.c:6: error: (Each undeclared identifier is reported only once
hello.c:6: error: for each function it appears in.)
hello.c:6: error: ‘window’ undeclared (first use in this function)
hello.c:10: error: ‘GTK_WINDOW_TOPLEVEL’ undeclared (first use in this function)
ต้องติดตั้งโปรแกรมใดเพิ่มเติม และติดตั้งอย่างไร แนะนำด้วยครับ
(แต่แรกผมลองใน Ubuntu 7.04 แล้วทำไม่ได้ นึกว่าเป็นเพราะ Ubuntu
จึงมาทดลองใน Debian ก็พบว่าเป็นปัญหาเหมือนกันครับ)
ขอบคุณครับ
- Log in to post comments
![]() |
ลิขสิทธิ์ของบทความเป็นของเจ้าของบทความแต่ละชิ้น ผลงานนี้ ใช้สัญญาอนุญาตของครีเอทีฟคอมมอนส์แบบ แสดงที่มา-อนุญาตแบบเดียวกัน 3.0 ที่ยังไม่ได้ปรับแก้ |
Re: ทดลอง compile ภาษา C ด้วย gtk
on 6 September, 2007 - 18:21 Permalink
ต้องลงแพกเกจชื่อ
pkg-config
เพิ่มครับ$ sudo aptitude install pkg-config
สามารถค้นหาแพกเกจด้วยคำสั่ง
$ aptitude search pkg-config
หรือ
$ apt-cache search pkg-config
ครับ
Re: ทดลอง compile ภาษา C ด้วย gtk
on 6 September, 2007 - 20:00 Permalink
อย่าลืม libgtk2.0-dev ด้วยเด้อครับ
Re: ทดลอง compile ภาษา C ด้วย gtk
on 6 September, 2007 - 20:03 Permalink
แสดงว่า Header ที่ต้องการ include เข้ามา ไม่มีอยู่ หรือระบบหาไม่เจอ
ลงเข้าไปหน่อยละกันครับผม
ลองใหม่อีกครั้ง น่าจะผ่าน
ปล. code ที่เอาเข้ามาแปะ ใส่ tag <code> ... CODE ... </code> คร่อมไว้ด้วยก็ดีครับ
อีกอย่างคือ เรื่อง HTML Escape Character
___
Neutron: Linux Addict!
Re: ทดลอง compile ภาษา C ด้วย gtk
on 7 September, 2007 - 09:34 Permalink
ผมติดตั้ง pkg-config เป็นอันดับแรก
ด้วยคำสั่ง
# aptitude install pkg-config
ต่อมาสั่ง compile ใหม่ ด้วยคำสั่ง
gcc hello.c -o hello `pkg-config --cflags --libs gtk+-2.0`
ผลที่ได้เป็นดังนี้ครับ
-----------------------------------------
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
hello.c:1:21: error: gtk/gtk.h: No such file or directory
hello.c: In function ‘main’:
hello.c:6: error: ‘GtkWidget’ undeclared (first use in this function)
hello.c:6: error: (Each undeclared identifier is reported only once
hello.c:6: error: for each function it appears in.)
hello.c:6: error: ‘window’ undeclared (first use in this function)
hello.c:10: error: ‘GTK_WINDOW_TOPLEVEL’ undeclared (first use in this function)
-----------------------------------------
ผมจึงติดตั้ง เพิ่มเติมดังนี้
# aptitude install libgtk2.0-dev
ต่อมาสั่ง compile ใหม่ ด้วยคำสั่ง
gcc hello.c -o hello `pkg-config --cflags --libs gtk+-2.0`
ผลที่ได้กลับเงียบสนิท ไม่มีอะไรเกิดขึ้นเลย
ไม่มีหน้าต่างที่เป็นผลของโปรแกรมปรากฎให้เห็น
ไม่มี error อะไรแจ้งด้วย แปลกมากๆ ครับ
ย้อนกลับไปอ่าน error ตอนต้น เขาแนะนำอะไรเกี่ยวกับไฟล์ gtk+-2.0.pc
และต้องระบุแจ้งให้ PKG_CONFIG_PATH ทราบด้วยว่า ไฟล์ดังกล่าวอยู่ใน directory อะไร
ทีนี้.. ต้องทำอย่างไรดีครับ
ไฟล์ gtk+-2.0.pc ก็สร้างไม่เป็น
แล้วจะไปกำหนดให้ PKG_CONFIG_PATH รู้อีก จะกำหนดอย่างไรละครับ
แนะนำให้ด้วยครับ ขอบคุณครับ
Re: ทดลอง compile ภาษา C ด้วย gtk
on 7 September, 2007 - 10:41 Permalink
อาการแบบนี้น่าจะเรียกว่า "คอมไพล์ผ่าน" มังครับ
ลอง ls ดู น่าจะมีไฟล์ชื่อ hello ซึ่ง execute ได้ ถ้ามีก็ลองเรียกดูนะครับ:
Re: ทดลอง compile ภาษา C ด้วย gtk
on 7 September, 2007 - 10:44 Permalink
เป็นเช่นนั้นจริงๆ ครับ
แรกผมก็คิดว่าน่าจะมีอะไรบอกกันมั้ง
นี่เล่นไม่บอกอะไรกันเลย
Re: ทดลอง compile ภาษา C ด้วย gtk
on 7 September, 2007 - 10:01 Permalink
ปกติ ลง libgtk2.0-dev แล้วก็เรียบร้อยนะครับ
ลองตรวจสอบดูอย่างนี้ก็ได้ ว่า libgtk2.0-dev เข้ามาในระบบหรือยัง
จะประกฏ
หรือถ้ายังไม่ขึ้น (ไม่น่าเกิด...ถ้าลง libgtk2.0-dev อย่างถูกต้องเรียบร้อย)
แล้วลอง compile ใหม่ น่าจะผ่านแล้วครับ
แต่คิดว่าครั้งต่อไป คงไม่ต้องสั่งอีก เพราะระบบจัดการให้แล้ว
(จริง ๆ คิดว่าลอง Logout แล้ว Login ใหม่ก็น่าจะช่วยได้แล้วครับผม แต่อธิบายให้เห็นโครงสร้างของ pkgconfig path เฉย ๆ)
___
Neutron: Linux Addict!
Re: ทดลอง compile ภาษา C ด้วย gtk
on 7 September, 2007 - 10:41 Permalink
ขอบคุณครับ ใช้งานได้ละ
โดยสรุป
1. ติดตั้ง # aptitude install pkg-config
2. ติดตั้ง # aptitude install libgtk2.0-dev
ตรวจสอบด้วย $ pkg-config --list-all | grep gtk+-2.0
ลอง compile ใหม่ ได้ผลเป็นที่เรียบร้อย
ขอบคุณครับ
หมายเหตุ แต่ก็สงสัยเป็นอย่างมาก ว่า ทำไม debian เขาไม่ติดตั้งมาให้เสร็จเลย
หรือปัจจุบันถือว่าเป็นเรื่องที่เกินจำเป็นไปซะแล้ว สำหรับ ภาษา C กับ Gtk
Re: ทดลอง compile ภาษา C ด้วย gtk
on 7 September, 2007 - 10:51 Permalink
libgtk2.0-dev เป็น development package ซึ่งไม่ใช่ว่าทุกคนจำเป็นต้องใช้
ท่านใดต้องใช้ ก็ลงเองตามความสะดวกครับผม :)
___
Neutron: Linux Addict!