Picses' sky

Picses' sky
posts - 43, comments - 29, trackbacks - 0, articles - 24

win32下基于eclipse的GTK+编译环境[转载]

Posted on 2007-12-28 23:11 Matthew Chen 阅读(1133) 评论(0)  编辑  收藏 所属分类: Others
GTK+是遵循LGPL协议的一个优秀的GUI类库.基于C语言开发,是GNOME的基础,也是Linux下 GUI程序的首选.同时,GTK+也是一个跨平台的GUI库,win32下写"原生"GUI程序(商业的和非开源的也都不需付费),GTK+都是一个不错的选择! 本文基于我的实践,介绍了windows下用eclipse/CDT 来搭建GTK+编译环境的步骤,并成功编译运行了GTK+的"hello world"程序! 希望对那些想在win32下开发GTK+程序的朋友能有所帮助.
转载请注明出处: http://zhw618.blogchina.com


1. 为什么俺选 eclipse ?

    
eclipse 是一个开源的优秀的IDE,主要是用于开发调试Java程序.同时它也是一个平台,通过CDT插件可以提供一个完整的C/C++开发环境.eclipse 的主页是   http://www.eclipse.org ,上面可以下载到最新的 eclipse和CDT,而且还有中文语言包,相当的不错.
   
      开始也想过用其它的IDE,比如 Dev-cpp (主页 http://dev-cpp.sourceforge.net) ,可是它的编辑器只支持GBK中文编码,不支持UTF8. 而我们知道GTK+是支持国际化的,所有的c源文件最好是UFT8的. 如果.c文件是GBK编码,那么里面的中文,编译出来都是乱码.要想消除乱码,必须把文件中的所有中文string串通过下面这个函数来转换:
      g_locale_to_utf8("世界你好",-1,0,0,0)
这样很是麻烦.而eclipse支持多种编码,选用utf8来编辑源文件,就没有中文乱码问题啦,很是方便,呵呵

     eclipse的跨平台性也是我选择它的一个重要原因! 我们知道,linux下vim,emacs对于我们这些习惯了win下的编辑器的来说,还是有点麻烦的. eclipse可以运行在linux等平台上,这样,我们就可以在linux下使用同一个IDE--eclipse来开发啦,这样降低了win程序员编写 linux程序的门槛,呵呵.

     嘻嘻,总之,eclipse备受业界好评,应该也不会让我们失望的!再说,熟悉了eclipse,以后学习java的话IDE也不用重新去适应啦,^_^

2. win32下GNU 编译环境搭建
  
   
eclipse和dev-cpp这些都是调用MinGW或者Cygin来编译调试C/C++程序的. 建议使用MinGW,它全称是Minimal GNU for Win,是GNU开发工具在win32平台上的一个移植,不同于Cygin,MinGW是原生的win32移植,不需要另外的POSIX模拟中间层的支持.另外MinGW项目也发布了一个最小的posix实现接口--msys,移植了很多linux下很方便的小工具,比如ls,vi,rvxt等等. MinGW项目的主页是
     http://www.mingw.org
我们可以在上面下载最新的   MinGW-3.2.0-rc-3.exe 和 MSYS-1.0.10.exe 这两个文件,安装的时候先安装Mingw,然后安装msys.安装完msys之后会跳出一个问题,选y (yes), 然后它问你是不是已经安装了 MinGW, 选y,然后按它的例子输入MinGW的安装路径(比如c:/MinGW),这样msys就把这个路径mount到了安装路径的msys目录下,可以直接使用Mingw的那些工具了.

    好了.装好这两个以后我们就要eclipse编译c文件需要的gcc,gdb,make工具了.为了eclipse能找到它们,要把c:\MinGW\ bin目录加到windows的环境变量PATH中. 需要说明的是,c:\MinGW\bin目录下的make不是GNU的make,而是mingw32-make.exe,这个是make在windows 下的版本,由于没有posix完整接口,这个make缺少一些标准make的功能,同时也有一些linux下make所不具备的功能. 因为我们后面要用到pkg-config来自动配置GTK+库的路径,这是mingw32-make.exe所不支持的. msys中提供的GNU make是可以的,为了使用它,我们还得把msys安装目录下的bin目录加到环境变量PATH中.

3. 安装GTK+库的win32 port

     GTK在windows上的移植,目前常见的主要有两个项目:gimp-win和glade-win32,分别在sourceforge.net上有各自的主页.前一个是官方的,不过 GTK+-dev包不是完全打包好的,使用起来不是很方便,推荐用后一个.
     gimp-win的地址:
          http://www.gimp.org/~tml/gimp/win32/downloads.html
          http://sourceforge.net/projects/gimp-win/
     glade-win32的地址:
          http://sourceforge.net/projects/gladewin32/  

          去下载最新的gtk+-win32-dev 包,双击运行安装就好了.安装程序自动注册了环境变量,很方便.

4. 安装eclipse和CDT

      到 http://www.eclipse.org上下载eclipse和CDT,解压到同一个目录下,比如 D:\eclipse ,运行eclipse.exe 即可
      注意的是:   CDT要搭载对应的版本的eclipse使用,下载时候阅读说明选择正确版本的CDT即可

5. 编译GTK+版的hello world程序.

    打开eclipse,file-> new-> project, 里面多了c和c++两种工程类型,选择c,建一个managed make c project(自动产生makefile文件).
eclipse自动关联到c程序的视图,呵呵,真的是挺周到的.
     左边的工程文件夹那里选 new,新建一个 c source file, 把下面的这个hello world 程序拷贝进去,并保存.
################################################################
#include <gtk/gtk.h>

/* This is a callback function. The data arguments are ignored
* in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
                    gpointer    data )
{
     g_print ("Hello World\n");
}

static gboolean delete_event( GtkWidget *widget,
                               GdkEvent   *event,
                               gpointer    data )
{
     /* If you return FALSE in the "delete_event" signal handler,
      * GTK will emit the "destroy" signal. Returning TRUE means
      * you don't want the window to be destroyed.
      * This is useful for popping up 'are you sure you want to quit?'
      * type dialogs. */

     g_print ("delete event occurred\n");

     /* Change TRUE to FALSE and the main window will be destroyed with
      * a "delete_event". */

     return TRUE;
}

/* Another callback */
static void destroy( GtkWidget *widget,
                      gpointer    data )
{
     gtk_main_quit ();
}

int main( int    argc,
           char *argv[] )
{
     /* GtkWidget is the storage type for widgets */
     GtkWidget *window;
     GtkWidget *button;
    
     /* This is called in all GTK applications. Arguments are parsed
      * from the command line and are returned to the application. */
     gtk_init (&argc, &argv);
    
     /* create a new window */
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
     /* When the window is given the "delete_event" signal (this is given
      * by the window manager, usually by the "close" option, or on the
      * titlebar), we ask it to call the delete_event () function
      * as defined above. The data passed to the callback
      * function is NULL and is ignored in the callback function. */
     g_signal_connect (G_OBJECT (window), "delete_event",
               G_CALLBACK (delete_event), NULL);
    
     /* Here we connect the "destroy" event to a signal handler.  
      * This event occurs when we call gtk_widget_destroy() on the window,
      * or if we return FALSE in the "delete_event" callback. */
     g_signal_connect (G_OBJECT (window), "destroy",
               G_CALLBACK (destroy), NULL);
    
     /* Sets the border width of the window. */
     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    
     /* Creates a new button with the label "Hello World". */
     button = gtk_button_new_with_label ("世界你好!");
    
     /* When the button receives the "clicked" signal, it will call the
      * function hello() passing it NULL as its argument.   The hello()
      * function is defined above. */
     g_signal_connect (G_OBJECT (button), "clicked",
               G_CALLBACK (hello), NULL);
    
     /* This will cause the window to be destroyed by calling
      * gtk_widget_destroy(window) when "clicked".   Again, the destroy
      * signal could come from here, or the window manager. */
     g_signal_connect_swapped (G_OBJECT (button), "clicked",
                   G_CALLBACK (gtk_widget_destroy),
                               G_OBJECT (window));
    
     /* This packs the button into the window (a gtk container). */
     gtk_container_add (GTK_CONTAINER (window), button);
    
     /* The final step is to display this newly created widget. */
     gtk_widget_show (button);
    
     /* and the window */
     gtk_widget_show (window);
    
     /* All GTK applications must have a gtk_main(). Control ends here
      * and waits for an event to occur (like a key press or
      * mouse event). */
     gtk_main ();
    
     return 0;
}
######################################################
保存之后eclipse马上开始编译啦,有很多错误! 这是为什么啊?
不要着急, 这主要是eclipse不知道 GTK+的头文件路径,以及不知道与GTK的哪些库文件来连接.
解决这个问题有两种方法:
   1)   编辑makefile文件,把这些 路径以参数 -I(include路径) 和 -l(gtk的lib文件) 的方式告诉gcc
        编译时候指定include路径,gcc的参数有(假定GTK装在c:\GTK目录下):
        -IC:/GTK/include/gtk-2.0 -IC:/GTK/lib/gtk-2.0/include -IC:/GTK/include/atk-1.0 -IC:/GTK/include/pango-1.0                -IC:/GTK/include/glib-2.0 -IC:/GTK/lib/glib-2.0/include  
      
        连接时候需要指定 GTK+ 的lib库,gcc的参数有
      -LC:/GTK/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -liconv  
      
        有了这些路径参数就可以成功编译了.编辑makefile文件需要有make的知识,容易出错,我们也可以在eclipse的 project->properties(工程属性)
然后选 c/c++ build ,右边的tools setting 标签页里面 来一个一个填上这些 -I 路径和 -l库,目录和库不少,所以这样也比较麻烦.

   2)   用pkg-config 工具来帮忙生成 这些include路径和 lib库
        也可以用pkg-config 来帮忙生成 1) 中繁琐的gcc参数.pkg-config在安装GTK+-dev 时候已经有了,不需要额外安装. 我们可以在 cmd窗口中输入:   pkg-config   --cflags gtk+-2.0   输出的就是include路径的gcc参数, pkg-config   --libs gtk+-2.0 输出lib库的gcc参数.这个工具使用起来相当方便,make文件可以写成这样:
       gcc   -o hello.exe hello.c     `pkg-config   --cflags gtk+-2.0` `pkg-config   --libs gtk+-2.0`
然后make的时候,make命令就会自动用 pkg-config的输出替换掉 pkg-config命令(msys中的make.exe可以,而mingw32-make.exe不支持).

      下面我们就在eclipse里面指定这两个pkg-config命令,让eclipse帮我们生成的makefile可以找到GTK+ 的include文件和 lib包.
   1) project->properties(工程属性),然后选 c/c++ build ,右边的tools setting 标签页里面选 GCC C++ Complier, 这个项目下的选项供我们来设定一些gcc的编译参数.如下图所示
   
选择Miscellanous, 把`pkg-config   --cflags gtk+-2.0`填入other flags 的最后面. 如果还有其它选项也可以接在后面(后面我们还要添加一个 -mms-bitfields选项).
   
     2)连接选项配置. 还是上图,选GCC C++ Linker, 其下的选项供我们配置连接选项.点击Miscellanous如下图所示

在Other objects 里面填上 `pkg-config   --libs gtk+-2.0` 命令.(填上后eclipse会自动又加一个" ",点编辑,去掉这对引号).
经我试验, 要是填在最上面的 linker flags栏里面的话 , `pkg-config   --libs gtk+-2.0`选项是放在紧跟gcc后的第一个选项,
make时候不会自动替换成它的输出(俺也不知道为何),   而other   objects 里面的选项是放在gcc   命令的最后面的,make的时候就可以自动用输出替换,真是奇怪.呵呵.
      还有,如果想让编译出来的程序没有那个cmd的dos窗口,还需要加一个连接选项 -mwindows (如上图中示).这个不管加上边还是下边都可以.

点击apply以后,eclipse又开始自动编译啦.这下什么错误也没有了.   到目录树下debug目录下查看,多了hello.o 和 hello.exe文件.
双击hello.exe运行程序, 呓??? 出来了个错误窗口,说需要加 -mms-bitfields 编译参数!!
返回来加上这个参数(图1中所示),保存,自动编译,重新运行hello.exe,这下一切OK,我们的第一个GTK+程序诞生了,呵呵,太高兴了!!
                                                                   
enjoy GTK! good lucy!


只有注册用户登录后才能发表评论。


网站导航: