本文介绍了如何在Linux上用GCC编译C和Gtk +?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过,但我没有收到我真正想要的信息。是否有人能够尽可能完全和根本地解释如何在Linux上使用GCC编写Gtk +代码时编译。有些东西像反引号,c99和.o文件,我根本听不懂。

我也很感谢学习Gtk +代码的任何资源。我发现的所有源代码都是针对2.x版的,但我认为3.6是当前版本。



我想重申一下,我只对在C代码中。请不要尝试向我解释C ++或C#的好处,我已经阅读了所有这些内容。对于初学者,你编写你的C代码,比如说hello_world_gtk.c ,然后通过使用适当的编译器和链接器标志来编译并链接到Gtk。这些标志由pkg-config工具提供给您。要获得所需的编译器标志,可以使用以下命令调用该工具:

 
pkg-config gtk + -2.0 --cflags

pre
$ b $ p



 
pkg-config gtk + -2.0 - -libs

反引号允许您从pkg-config获取输出并将其作为参数传递给gcc:

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

但是您不需要使用反引号。您可以手动复制和粘贴标志。



对于Gtk 3,将2.0替换为3.0。如果pkg-config报告无法找到软件包,那么您没有安装Linux发行版提供的Gtk开发包。



如果您通常不会不了解如何将某些东西编译成目标文件然后将它链接起来,那么你不应该从Gtk开始,而应该用普通的C代替。熟悉编译和链接的绝对基础知识后,即可转入Gtk应用程序。


I've searched and searched but I'm not getting the information I really want. Can someone please explain, as completely and fundamentally as possible, how Gtk+ code is compiled when writing in C, using GCC, on Linux. There's things like backticks, "c99", and .o files that I don't understand at all.

I'd also appreciate any resources for learning Gtk+ code. All the sources I've found are for versions 2.x, but I think 3.6 is the current version.

I'd like to reiterate, I'm only interested in C code. Please don't try explaining to me the benefits of C++ or C#, I've read all of them. I'm here for C. Thank you!

解决方案

For starters, you write your C code, say "hello_world_gtk.c", and then you compile and link it against Gtk, by using appropriate compiler and linker flags. These flags are given to you by the pkg-config tool. To get the needed compiler flags, you call that tool with:

pkg-config gtk+-2.0 --cflags

For the link flags:

pkg-config gtk+-2.0 --libs

The backticks allow you to get the output from pkg-config and pass it on as arguments to gcc:

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

But you're not required to use backticks. You can just copy&paste the flags manually if you want.

For Gtk 3, replace "2.0" with "3.0". If pkg-config reports that it couldn't find the package, then you didn't install the Gtk development package as offered by your Linux distribution.

If you generally don't understand how you compile something into object files and then link it, then you shouldn't begin with Gtk, but with plain C instead. Once you're familiar with the absolute basics of compiling and linking, then you can move on to Gtk applications.

这篇关于如何在Linux上用GCC编译C和Gtk +?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:10