一直都写服务端,对计算机的运行原理有了一定了解,就想看看自己对其它领域程序的理解是不是可以举一反三。
最简单的GTK
程序就是官网入门示例了,安装GTK
的过程就不在这描述。代码如下:
// hello.c
#include <gtk/gtk.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Hello");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
gtk_window_set_child (GTK_WINDOW (window), button);
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
编译:
$ gcc hello.c -o hello `pkg-config --cflags --libs gtk4`
创建后,其实就可以通过./hello
来运行程序,但有个弊端,就是运行需要命令行,不符合一般用户的使用习惯。
我们的习惯一般都是双击hello
运行的,那么在macOS
下,创建这种程序的方法如下:
$ mkdir -p hello.app/Contents/MacOS
$ mv hello hello.app/Contents/MacOS
$ touch hello.app/Contents/Info.plist
Info.plist
文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>executable_name</key>
<string>rand_string</string>
<key>unique_id</key>
<string>rand_string</string>
</dict>
</plist>
其中<key>executable_name</key>
是可执行文件的名称,这里填hello
即可,<string>rand_string</string>
随便填些字符就好,<key>unique_id</key>
是程序的唯一ID
。
不过目前发现Info.plist
似乎不是必要的。
现在hello.app
就可以双击运行了,如无意外,会弹出一个窗口,内容为Hello World
。