在Ubuntu14.04上编写第一个GTK+3.

写于2014.7.30,热晨

前期

要安装好所需的东西:Ubuntu14.04系统,GCC编译器,GTK+3库,GNU make,vim文字编辑器.这些东西,我已在前博文中介绍了,请自行查看.

实施

打开终端,我们首先在/home目录下创建个文件夹pro,用于作为我们项目内容的存放点:

~$ mkdir pro

创建好后,就进入pro的目录下,以便我们接下来的操作:

~$ cd pro 

我们需要建立我们的makefile:

~/pro$ vim makefile

进入vim的界面后,点i,告诉vim我们使用插入模式,这样就可编辑makefile的内容了,我们输入以下内容:

LIBS =`pkg-config --libs gtk+-3.0`
CFLAGS =`pkg-config --cflags gtk+-3.0`
objs = H.o
edit : $(outs)
        cc -o edit $(objs) $(LIBS)
H.o : H.c
        cc -c H.c $(CFLAGS)
clean :
        rm edit $(objs)

点ESC键,再输入:wq,保存及退出vim,这样我们就做好了我们程序的自动编译器了.

我们再建立H.c文件:

~/pro$ vim H.c

我们进入vim后,同样的输入内容:

#include <stdio.h>
#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
    GtkWidget    *window;
    GtkWidget    *label;

    gtk_init(&argc,&argv);

    /* create the main, top level, window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    /* give it the title */
    gtk_window_set_title(GTK_WINDOW(window),"Hello World");

    /* connect the destroy signal of the window to gtk_main_quit
     * when the window is about to be destroyed we get a notification and
     * stop the main GTK+ loop
     */
    g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);

    /* create the "Hello, World" label */
    label = gtk_label_new("Hello, World");

    /* and insert it into the main window */
    gtk_container_add(GTK_CONTAINER(window),label);

    /* make sure that everything, window and label, are visible */
    gtk_widget_show_all(window);

    /* start the main loop, and let it rest until the application is closed */
    gtk_main();

    return 0;
}

同样,保存并退出vim.

这样我们就完成好,GTK+3程序编写了,我们需要通过GNU make进行自动编译:

~/pro$ make

如果没有意外,我们可以查看目录文件是否产生了*.out和可执行文件:

~/pro$ ls

如果看到edit文件和其他的*.out,我们就可以执行它:

~/pro$ ./edit

我们就可以看到,一个HelloWorld的窗口.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值