在Eclipse中编译C / GTK [英] Compiling C/GTK within Eclipse

查看:183
本文介绍了在Eclipse中编译C / GTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C / C ++版本的Eclipse来构建一个简单的GTK应用程序。但是,我似乎无法从Eclipse中编译一个GTK示例。我可以编译一个简单的Hello World样式测试应用程序,所以我知道工具链本身正在工作。但是,当我开始添加GTK的时候,编译器会出现错误。有趣的是,我可以在Eclipse环境之外编译示例。例如,我使用这个页面,并按照给出的说明让我构建一个工作的二进制文件。



我认为第一个问题是当我尝试在Eclipse中编译时,主要的GTK include文件被引用不同。非Eclipse版本可以使用(如示例)编译:

  #include< gtk / gtk.h> 

但是,在Eclipse中,这不起作用。我需要将其更改为:

  #include> gtk-2.0 / gtk / gtk.h> 

然后可以找到包含文件,但是编译过程然后开始抛出关于GtkWidget类型的错误。例如:

  #include> gtk-2.0 / 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 0;
}

导致这些错误:

  make all 
构建文件:../src/main.c
调用:GCC C编译器
gcc -O0 -g3 -Wall -c -fmessage-length = 0 -MMD -MP -MFsrc / main.d-MTsrc / main.d-osrc / main.o../src/main.c
../src/main.c:在函数'main'中:
../src/main.c:7:错误:'GtkWidget'未声明(在此函数中首次使用)
../src/main.c:7:错误:(每个未声明的标识符仅报告一次
../src/main.c:7:错误:对于其中显示的每个功能)
../src/main.c:7:error:'window'undeclared(在此函数中首次使用)
../src/main.c:9:warning:隐式声明函数'gtk_init'
../src/main.c:11:warning:隐式声明函数'gtk_window_new'
../src/main.c:11:错误:'GTK_WINDOW_TOPLEVEL'未声明(在此函数中首次使用)
../src/main.c:12:warning:隐式声明函数'gtk_widget_show'
../src/mai nc:14:warning:隐式声明函数'gtk_main'
make:*** [src / main.o]错误1

不知道该怎么做任何帮助将非常感谢。

解决方案

右键单击Eclipse项目并选择属性。从配置下拉列表中,选择 [所有配置] 。然后在工具设置选项卡上选择 GCC C编译器(默认),并将以下内容添加到结尾命令行模式(专家设置)框:

 `pkg-config --cflags --libs gtk + -2.0` 

GCC C Linker 选项执行同样的操作。



如果您不想使用gtk-2.0启动包含路径,也可以像建议的aardvark一样添加include目录(/usr/include/gtk-2.0)。


I'm fiddling around with the C/C++ version of Eclipse to build a simple GTK app. However, I can't seem to be able to compile a GTK sample from within Eclipse. I can compile a simple Hello World style test app, so I know the tool chain itself is working. However, the moment I start adding GTK into the mix the compiler comes up with errors. The funny thing is that I can compile the examples outside the Eclipse environment just fine. E.g., I'm using the examples on this page and following the instructions given there let me build a working binary.

I think the first problem is that the main GTK include file is referenced differently when I try to compile within Eclipse. The non-Eclipse version I can compile with (as in the example):

#include <gtk/gtk.h>

However, within Eclipse this doesn't work. I need to change it to:

#include <gtk-2.0/gtk/gtk.h>

The include file can then be found but the compilation process then starts to throw errors about the GtkWidget type. E.g.:

#include <gtk-2.0/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 0;
}

Results in these errors:

make all 
Building file: ../src/main.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.c"
../src/main.c: In function ‘main’:
../src/main.c:7: error: ‘GtkWidget’ undeclared (first use in this function)
../src/main.c:7: error: (Each undeclared identifier is reported only once
../src/main.c:7: error: for each function it appears in.)
../src/main.c:7: error: ‘window’ undeclared (first use in this function)
../src/main.c:9: warning: implicit declaration of function ‘gtk_init’
../src/main.c:11: warning: implicit declaration of function ‘gtk_window_new’
../src/main.c:11: error: ‘GTK_WINDOW_TOPLEVEL’ undeclared (first use in this function)
../src/main.c:12: warning: implicit declaration of function ‘gtk_widget_show’
../src/main.c:14: warning: implicit declaration of function ‘gtk_main’
make: *** [src/main.o] Error 1

Not sure how to go about this. Any assistance would be very much appreciated.

解决方案

Right click the Eclipse project and select properties. From the Configuration drop down, select [ All configurations ]. Then on the Tool Settings tab select GCC C Compiler (default) and add the following to the end Command line pattern (Expert settings) box:

`pkg-config --cflags --libs gtk+-2.0`

Do the same thing for the GCC C Linker option.

If you don't want to start your include paths with gtk-2.0 than also add the include directory (/usr/include/gtk-2.0) like aardvark suggested.

这篇关于在Eclipse中编译C / GTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆