在 Eclipse 中编译 C/GTK

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1315091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:53:38  来源:igfitidea点击:

Compiling C/GTK within Eclipse

eclipsegccgtk

提问by Luke

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 thispage and following the instructions given there let me build a working binary.

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

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):

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

#include <gtk/gtk.h>

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

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

#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.:

然后可以找到包含文件,但编译过程开始抛出关于 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;
}

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.

不知道该怎么做。任何帮助将不胜感激。

采纳答案by Luke

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:

右键单击 Eclipse 项目并选择属性。从配置下拉列表中,选择[ All configurations ]。然后在工具设置选项卡上选择GCC C Compiler(默认)并将以下内容添加到末尾Command line pattern(专家设置)框:

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

Do the same thing for the GCC C Linkeroption.

GCC C Linker选项做同样的事情。

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.

如果您不想使用 gtk-2.0 开始包含路径,那么还可以像 aardvark 建议的那样添加包含目录 (/usr/include/gtk-2.0)。

回答by Bruce Alderman

Try adding the gtk directory to the build path:

尝试将 gtk 目录添加到构建路径:

Go into project Properties -> C/C++ build -> Settings -> Tool settings -> Directories and add it under Include paths.

进入项目属性 -> C/C++ 构建 -> 设置 -> 工具设置 -> 目录并将其添加到包含路径下。

回答by Petri Tuononen

A long waited relief considering the pkg-config support for Eclipse CDT is coming soon.

考虑到对 Eclipse CDT 的 pkg-config 支持,期待已久的解脱即将到来。

The support is under development and will be integrated to CDT around August or so.

该支持正在开发中,将在 8 月左右集成到 CDT。

The most important feature is that an easy to use user interface allows to select the required packages.

最重要的功能是易于使用的用户界面允许选择所需的包。

Project website: http://code.google.com/p/pkg-config-support-for-eclipse-cdt/

项目网站:http: //code.google.com/p/pkg-config-support-for-eclipse-cdt/

Update: Feel free to test the beta version:
http://marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
Feedback is appreciated!

更新:请随意测试测试版:
http: //marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
非常感谢您的反馈!