代码
#include <gtk/gtk.h>
gint count=1;
void on_button_clicked(GtkWidget *button,GdkEvent* event,gpointer userdata)
{ g_print("Hello, this is a Hello function test.\n");
g_print("%d\n",(gint)userdata);
count = count + 1 ;
}
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(gtk_main_quit),NULL);
gtk_window_set_title(GTK_WINDOW(window),"Hello Function");
gtk_window_set_default_size(GTK_WINDOW(window),500,100);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window),40);
button = gtk_button_new_with_label("Press this Button will show a message in terminal");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)count);
gtk_container_add(GTK_CONTAINER(window),button);
}
编译通过,运行无窗口
编译命令 gcc c.c `pkg-config --cflags --libs gtk+-2.0`或 gcc c.c `pkg-config --cflags --libs gtk+-3.0`
刚开始编译失败fail to load moudle
于是sudo apt-get install libcanberra-gtk-module
我的系统ubuntu22.04