我用homebrew下載安裝好了gtk+3,然後試著編譯了一個網上的示例代碼:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
static gboolean delete_event(GtkWidget * widget, GdkEvent * event,
gpointer data) {
gtk_main_quit();
return FALSE;
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "RobberPhex");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
g_signal_connect(window, "delete-event", G_CALLBACK(delete_event), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
label = gtk_label_new("Hello GTK!");
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show(label);
gtk_widget_show(window);
gtk_main();
return 0;
}
用gcc編譯:
gcc testGTK.c -o testGTK `pkg-config --cflags --libs gtk+-3.0`
結果運行的時候出錯了,錯誤信息是:
Dynamic session lookup supported but failed: launchd did not provide a socket path, verify that org.freedesktop.dbus-session.plist is loaded!
(testGTK:62547): Gtk-WARNING **: cannot open display:
結果在Google找了好久都沒找到為什麼會這樣 T T
我去網上找了一下load了d-bus然後只剩下
Gtk-WARNING **: cannot open display:
錯誤了,然後我感覺是因為我安裝完gtk+3和相關的組件之後沒有重啟,所以有的組件還沒有被load,於是我重啟了一下電腦再編譯運行就一切正常了
抱歉打擾大家的時間線了,希望之後遇到和我一樣問題的同學可以借鑒一下