The simplest window

最简单的窗口

目的:创建一个最简单的窗口,并显示出来。

效果:

clip_image002

源码main.c

#include <gtk/gtk.h>

int main(int argc ,char *argv[])
{

GtkWidget *window;

gtk_init(&argc,&argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

gtk_widget_show(window);

gtk_main();

return FALSE;
}

编译为:


gcc -o hello main.c `pkg-config --libs --cflags gtk+-2.0`

install gtk on Ubuntu Distribution

ubuntu上安装gtk

Refer from: http://www.cnblogs.com/niocai/archive/2011/07/15/2107472.html

我利用此方法成功在UBUNTU 10.04下安装GTK 2.20.1

一、安装

  1、安装gcc/g++/gdb/make 等基本编程工具

$sudo apt-get install build-essential

  2、安装 libgtk2.0-dev libglib2.0-dev 等开发相关的库文件

$sudo apt-get install gnome-core-devel

  3、用于在编译GTK程序时自动找出头文件及库文件位置  

$sudo apt-get install pkg-config

  4、安装 devhelp GTK文档查看程序

$sudo apt-get install devhelp

  5、安装 gtk/glib API参考手册及其它帮助文档

$sudo apt-get install libglib2.0-doc libgtk2.0-doc

  6、安装基于GTK的界面GTK是开发Gnome窗口的c/c++语言图形库 

$sudo apt-get install glade libglade2-dev

或者

$sudo apt-get install glade-gnome glade-common glade-doc

  7、安装gtk2.0 或者 gtk+2.0所需的所有文件统通下载安装完毕

$sudo apt-get install libgtk2.0-dev

或者

$sudo apt-get install libgtk2.0*

  

二、查看GTK库版本

  1、查看1.2.x版本

$pkg-config –modversion gtk+

  2、查看 2.x 版本

$pkg-config –modversion gtk+-2.0

  3、查看pkg-config的版本

$pkg-config –version

  4、查看是否安装了gtk

$pkg-config –list-all grep gtk

  

三、测试程序

 

//Helloworld.c

#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
;
}

 

  

四、编译运行

  1、编译

$gcc -o Helloworld Helloworld.c `pkg-config –cflags –libs gtk+-2.0`

  2、运行

$./Helloworld

 

Application Development using the Gnome Desktop

使用Anjuta开发一个简单的C++小程序

1 The Anjuta IDE 

You have probably used an IDE such as Code::Blocks, Eclipse, Netbeans, or Microsoft Visual Studio .NET (free version). There is an IDE named Anjuta DevStudio designed for Gnome. It is built from the GTK+ tool set. GTK+ is a highly usable, feature rich toolkit for creating graphical user interfaces which boasts cross platform compatibility and an easy to use API. GTK+ it is written in C, but has bindings to many other popular programming languages such as C++, Python and C# among others. GTK+ is licensed under the GNU LGPL 2.1 allowing development of both free and proprietary software with GTK+ without any license fees or royalties.

Anjuta DevStudio is a versatile software development studio featuring a number of advanced programming facilities including project management, application wizard, interactive debugger, source editor, version control, GUI designer, profiler and many more tools. It focuses on providing simple and usable user interfaces, yet powerful for efficient development. This chapter will show you how to use Anjuta to create a simple, console-based, program in C++.

2 Building and Running a Simple C++ Program 

Choose Anjunta from the Gnome “Development/Integrated Environment” menu.

clip_image001 

This is the account for the user tutorial upon which we have based all our tutorial material.

clip_image002 

This is what you will see when you first run Anjuta. Select Project so we can create our first C++ project.

clip_image003 

In this dialog, choose the C++ tab and select “Generic C++”.

clip_image004 

Change “Author”, “Email address”, and “Version” as you wish and then choose a project name. The default supplied by Anjuta is “foobar-cpp” (fouled-up beyond all recognition”). We choose to name our project “first-anjuta-project” and move on.

clip_image005

Next you are presented with a choice of “Project options”. It is wise to just leave everything here alone and move forward.

clip_image006

Before creating the project, Anjuta summarizes the project parameters. Now click Apply.

clip_image007

What you see in the left panel is the uninitialized project. Expand “src” to view the C++ source files that were built by default.

clip_image008 

Double-click “main.cc”.

clip_image009

The file “main.cc” is the standard C++ “Hello world!” source code along with comments about the GNU General Public License. Leave the comments at your option.

clip_image010

We do not want the “Hello World!” program, so change the source code as indicated.

clip_image011

The project cannot be built until it is configured. In the Build menu, select Configure Project…

clip_image012

Press Execute.

clip_image013

Anjuta will configure the project by building some files, including configure and Makefile. Recall that we discussed these in Chapter Seven.

clip_image014 

Now click the Execute icon and the program will run in a panel. Note that Anjuta has an integrated debugger. For larger programs, a tool like this is indispensible.

clip_image015 

Many features of Anjuta can be configured by selecting the Preferences menu item from the Edit menu.

参考:https://pacific.rsmart.com/access/content/user/cstutorials/Linux-Tutorial/linux13.html

undefined reference to GTK_TEXT

undefined reference to GTK_TEXT

出错信息:

undefined reference to GTK_TEXT

解决方法:

#include前定义:#define GTK_ENABLE_BROKEN

原因:

现在的Gtk+中默认不支持GtkText构件,而使用GtkTextView构件来代替。如果要使用GtkText构件,则必须在包含gtk.h头文件前定义GTK_ENABLE_BROKEN#define GTK_ENABLE_BROKEN

glib data type

Glib数据类型

       Glib提供了自己的一组在GTK+GNOME开发中使用的替代标准C数据类型的数据类型。这样做的原因不仅是便于阅读和使用,而且还便于移植。不管在何种平台上编译应用程序,许多平台对数据的尺寸和运算都会具有许多相同的限制。Glib数据类型保证不管什么样的系统和平台,都是相同的大小。

       Glib的数据类型

数据类型

说明

gboolean

真假或开关的值,一般与Glib定义的TRUEFALSE常量使用

gpointer

对应与void *,但是可读性更强

gcharguchar

对应一个字符和一个无符号字符

gintguintgshort

取代标准c的相应类型

glonggulong

长度根据硬件和操作系统体系结构的不同而变化

gint8guint8gint16

guint16gint32guint32

gint64guint64

具有已知长度的有符号和无符号整数数据类型。对于64位有符号和无符号整数值,仅在范围有限的机器上支持

gfloatgdouble

取代c语言提供的floatdouble

gsize

用来保存数据结构和数据类型尺寸的无符号数据类型

gssize

用来保存数据结构和数据类型尺寸的有符号数据类型

       上述这些类型,可以保证一致性和可读性。不管编译应用程序的平台是什么,定义一组具有可保证长度的数据类型将是一种很好的想法,同时笔记guchar减少了击键次数(unsigned char)。

       测试了一下每种数据类型所占的字节数:

#include <glib.h>
gint main(gint argc,gchar *argv[])
{
    g_print("The length of gboolean is %dn",sizeof(gboolean));
    g_print("The length of gpointer is %dn",sizeof(gpointer));
    g_print("The length of gchar    is %dn",sizeof(gchar));
    g_print("The length of guchar   is %dn",sizeof(guchar));
    g_print("The length of gint     is %dn",sizeof(gint));
    g_print("The length of guint    is %dn",sizeof(guint));
    g_print("The length of gshort   is %dn",sizeof(gshort));
    g_print("The length of glong    is %dn",sizeof(glong));
    g_print("The length of gulong   is %dn",sizeof(gulong));
    g_print("The length of gint8    is %dn",sizeof(gint8));
    g_print("The length of guint8   is %dn",sizeof(guint8));
    g_print("The length of gint16   is %dn",sizeof(gint16));
    g_print("The length of guint16  is %dn",sizeof(guint16));
    g_print("The length of gint32   is %dn",sizeof(gint32));
    g_print("The length of guint32  is %dn",sizeof(guint32));
    g_print("The length of gint64   is %dn",sizeof(gint64));
    g_print("The length of guint64  is %dn",sizeof(guint64));
    g_print("The length of gfloat   is %dn",sizeof(gfloat));
    g_print("The length of gdouble  is %dn",sizeof(gdouble));
    g_print("The length of gsize    is %dn",sizeof(gsize));
    g_print("The length of gssize   is %dn",sizeof(gssize));
    return 0;
}

clip_image002