ubuntu下code::blocks编译运行一个简单的gtk+2.0项目

在具体的操作之前,首先需要安装一些必要的软件。
ubuntu下默认安装了gcc,不过缺少必要的Header file,可以在命令行中输入下面的指令安装build-essential套件:
sudo apt-get install build-essential
使用GTK+2.0需要安装GTK开发套件,在命令行中输入下面的指令安装GTK开发套件libgtk2.0-dev:
sudo apt-get install libgtk2.0-dev
安装完成后,可以使用pkg-config查看一下GTK的相关编译环境信息:
pkg-config --cflags --libs gtk+-2.0
ubuntu安装code::blocks可以在命令行中输入如下指令来完成:
sudo apt-get install codeblocks

程序helloworld.c演示了如何开启一个桌面的窗口程序。

#include <gtk/gtk.h>
int main(int args, char *argv[]) {
    GtkWidget *window;
    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_show(window);
    gtk_main();
    return 0;
}
helloworld.c

我们可以在命令行输入如下信息来编译该程序:
gcc -o helloworld helloworld.c `pkg-config --cflags --libs gtk+-2.0`
在命令行输入如下命令将会看到出现了一个窗口:
./helloworld

在code::blocks中创建一个Empty Project(在这里我给项目取名为"HelloWorld"),在项目中新建一个main.c文件,其中的内容和上面的helloworld.c文件的内容是一样的。此时运行HelloWorld项目并不能正常运行,在Build messages中会提示如下错误:
fatal error: gtk/gtk.h: No such file or directory
这是因为我们没有像之前编译helloworld.c程序时提供必要的参数`pkg-config --cflags --libs gtk+-2.0`。
打开项目所在目录下的HelloWorld.cbp文件(这个文件类似makefile,里面包含了项目相关的一些配置信息),在project项的compiler项中添加
<Add option="`pkg-config gtk+-2.0 --cflags`" />
在project项中添加Linker项
<Linker>
    <Add option="`pkg-config gtk+-2.0 --libs`" />
</Linker>
再次运行Helloworld项目,能够得到之前手工编译运行helloworld.c相同的一个窗口。
完整的HelloWorld.cbp代码(添加GTK参数后):

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
    <FileVersion major="1" minor="6" />
    <Project>
        <Option title="HelloWorld" />
        <Option pch_mode="2" />
        <Option compiler="gcc" />
        <Build>
            <Target title="Debug">
                <Option output="bin/Debug/HelloWorld" prefix_auto="1" extension_auto="1" />
                <Option object_output="obj/Debug/" />
                <Option type="1" />
                <Option compiler="gcc" />
                <Compiler>
                    <Add option="-g" />
                </Compiler>
            </Target>
            <Target title="Release">
                <Option output="bin/Release/HelloWorld" prefix_auto="1" extension_auto="1" />
                <Option object_output="obj/Release/" />
                <Option type="1" />
                <Option compiler="gcc" />
                <Compiler>
                    <Add option="-O2" />
                </Compiler>
                <Linker>
                    <Add option="-s" />
                </Linker>
            </Target>
        </Build>
        <Compiler>
            <Add option="`pkg-config gtk+-2.0 --cflags`" />
            <Add option="-Wall" />
        </Compiler>
        <Linker>
            <Add option="`pkg-config gtk+-2.0 --libs`" />
        </Linker>
        <Unit filename="main.c">
            <Option compilerVar="CC" />
        </Unit>
        <Extensions>
            <code_completion />
            <debugger />
        </Extensions>
    </Project>
</CodeBlocks_project_file>
HelloWorld.cbp


学习资料:
Ubuntu 下安裝 GTK http://openhome.cc/Gossip/GTKGossip/GTKUnderUbuntu.html
编程语言:gtk程序设计简介 http://www.turbolinux.com.cn/turbo/wiki/doku.php?id=编程语言:gtk程序设计简介
GTK+2.0教程 http://runmediaprocess.googlecode.com/svn/trunk/dataBase/界面UI开发/GTK/GTK_2.0_Tutorial(中文版).pdf

posted @ 2016-07-12 10:32  月光诗人  阅读(964)  评论(0编辑  收藏  举报