Linux Eclipse CDT GTK+开发配置

最近在学习Eclipse中开发GTK+的程序,但是遇到了一点小问题 ,因为在Eclipse中并不能识别GTK+的存在,所以记录下编译过程

我的环境是 Ubuntu 14.0 64-bit 使用 GNU 的 G++ 4.9.1 编译器,IDE是 Eclipse IDE for C/C++ Developers ( Luna Service Release 1a (4.4.1) )

  1. 首先在eclipse中创建一个新项目,右击项目名,选择 Properties(Alt + Enter)
  2. 在 C/C++ Build 中选择 Setting ,找到右侧的 GCC C++ Compiler 下的 Includes
  3. 在其中加入如下的路径(该路径可由在控制台下执行 pkg-config –cflags gtk+-3.0 获得)

    /usr/include/gtk-3.0
    /usr/include/at-spi2-atk/2.0
    /usr/include/at-spi-2.0
    /usr/include/dbus-1.0
    /usr/lib/x86_64-linux-gnu/dbus-1.0/include
    /usr/include/gio-unix-2.0/
    /usr/include/mirclient
    /usr/include/mircommon
    /usr/include/cairo
    /usr/include/pango-1.0
    /usr/include/harfbuzz
    /usr/include/atk-1.0
    /usr/include/pixman-1
    /usr/include/freetype2
    /usr/include/gdk-pixbuf-2.0
    /usr/include/libpng12
    /usr/include/glib-2.0
    /usr/lib/x86_64-linux-gnu/glib-2.0/include

    GTK+ Setting GCC Compiler Includes

    GTK+ Setting GCC Compiler Includes

  4. 在选择GCC C++ Linker
  5. 在 Command line pattern 末尾加入 `pkg-config –libs gtk+-3.0`
    GTK+ Setting GCC Linker

    GTK+ Setting GCC Linker

  6. 然后点击OK就大功告成啦

测试代码如下:

/*
 * main.cpp
 *
 * Created on: Jun 15, 2015
 * Author: dlll
 */

#include <gtk/gtk.h>
void close_app( GtkWidget *window, gpointer data )
{
	gtk_main_quit(); /* 退出gtk_main */
}


int main( int argc, char *argv[] )
{
	GtkWidget	*window;
	GtkWidget	*button;

	gtk_init( &argc, &argv );
	window = gtk_window_new( GTK_WINDOW_TOPLEVEL );

	gtk_window_set_title( GTK_WINDOW( window ), "My first GTK" );
	gtk_window_set_default_size( GTK_WINDOW( window ), 200, 200 );
	gtk_window_set_position( GTK_WINDOW( window ), GTK_WIN_POS_MOUSE );

	/* 将destroy信号绑定close_app函数 */
	g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK( close_app ), NULL );
	button = gtk_button_new_with_label( "Hello World" );
	gtk_container_add( GTK_CONTAINER( window ), button );
	g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( gtk_main_quit ), NULL );

	gtk_widget_show_all( window );
	gtk_main();

	return(1);
}

您可能还喜欢...

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据