ubuntu 에서 gtk 설치 & sample code

ubuntu 에서 gtk 설치 & sample code

| 공개

- gtk 2 설치

apt-get install libgtk2.0-dev 를 실행시킨 후 Y를 입력한다.

(root 로그인이 아닌 경우는 sudo apt-get install libgtk2.0-dev )

 

- gtk 3 설치

#apt-get install libgtk-3-dev

 

 

- BackTrack5 R2 KDE 환경에서 설치함.

 

- 설치가 잘 되었는지 sample code를 만들어 보자. (http://www.levien.com/gimp/hello.html)

아래 코드를 hello.c 로 저장한다.

 

#include <gtk/gtk.h>


void

hello (void)

{

  g_print ("Hello World\n");

}


void

destroy (void)

{

  gtk_main_quit ();

}


int

main (int argc, char *argv[])

{

  GtkWidget *window;

  GtkWidget *button;


  gtk_init (&argc, &argv);


  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_signal_connect (GTK_OBJECT (window), "destroy",

      GTK_SIGNAL_FUNC (destroy), NULL);

  gtk_container_border_width (GTK_CONTAINER (window), 10);


  button = gtk_button_new_with_label ("Hello World");


  gtk_signal_connect (GTK_OBJECT (button), "clicked",

      GTK_SIGNAL_FUNC (hello), NULL);

  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",

     GTK_SIGNAL_FUNC (gtk_widget_destroy),

     GTK_OBJECT (window));

  gtk_container_add (GTK_CONTAINER (window), button);

  gtk_widget_show (button);


  gtk_widget_show (window);


  gtk_main ();


  return 0;

}

 

- compile

# gcc hello.c -o hello `pkg-config --cflags --libs gtk+-2.0`

 

- execution

./hello

 

 

 

 

- 위의 소스 코드를 gtk+-3.0 으로 컴파일 하면 아래와 같이 에러가 발생한다.

 

# gcc hello.c -o hello `pkg-config --cflags --libs gtk+-3.0`

/tmp/ccxtLdWx.o: In function `main':

hello.c:(.text+0x56): undefined reference to `GTK_SIGNAL_FUNC'

hello.c:(.text+0x64): undefined reference to `GTK_OBJECT'

hello.c:(.text+0x80): undefined reference to `gtk_signal_connect'

hello.c:(.text+0xa5): undefined reference to `gtk_container_border_width'

hello.c:(.text+0xc1): undefined reference to `GTK_SIGNAL_FUNC'

hello.c:(.text+0xcf): undefined reference to `GTK_OBJECT'

hello.c:(.text+0xeb): undefined reference to `gtk_signal_connect'

hello.c:(.text+0xf7): undefined reference to `GTK_OBJECT'

hello.c:(.text+0x105): undefined reference to `GTK_SIGNAL_FUNC'

hello.c:(.text+0x113): undefined reference to `GTK_OBJECT'

hello.c:(.text+0x12b): undefined reference to `gtk_signal_connect_object'

collect2: error: ld returned 1 exit status

>> deprecated 함수들이 존재함.

 

- gcc hello.c -o hello `pkg-config --cflags --libs gtk+-3.0` 로 빌드하기 위해서는 아래 코드를 사용하자.

 

#include <gtk/gtk.h> 

void hello(GtkWidget *widget,gpointer data) 

    g_print("Hello GClinux!\n"); 

}

 

gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data) 

    g_print ("delete event occurred\n"); 

    return(TRUE); 

}

 

void destroy(GtkWidget *widget,gpointer data) 

    gtk_main_quit(); 

 

int main( int argc, char *argv[] ) 

    GtkWidget *window; 

    GtkWidget *button; 

    gtk_init (&argc, &argv); 

    window=gtk_window_new (GTK_WINDOW_TOPLEVEL); 

    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL); 

    g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(destroy), NULL); 

    gtk_container_set_border_width (GTK_CONTAINER(window), 10); 

    button = gtk_button_new_with_label ("hello"); 

    g_signal_connect (G_OBJECT(button), "clicked",G_CALLBACK(hello), NULL); 

    g_signal_connect_swapped(G_OBJECT(button), "clicked",G_CALLBACK(gtk_widget_destroy),G_OBJECT(window)); 

    gtk_container_add (GTK_CONTAINER(window), button); 

    gtk_widget_show (button); 

    gtk_widget_show (window);    

    gtk_main();    

    return(0); 

}  

댓글

댓글 본문
작성자
비밀번호
graphittie 자세히 보기