如何在 Linux 上使用 GCC 编译 C 和 Gtk+? [英] How to compile C and Gtk+ with GCC on Linux?

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

问题描述

我搜索了又搜索,但没有得到我真正想要的信息.有人可以尽可能完整和根本地解释在 Linux 上使用 GCC 用 C 编写时 Gtk+ 代码是如何编译的.有些东西,比如反引号、c99"和 .o 文件,我根本不明白.

我也很感激任何学习 Gtk+ 代码的资源.我找到的所有来源都适用于 2.x 版本,但我认为 3.6 是当前版本.

我想重申一下,我只对 C 代码感兴趣.请不要试图向我解释 C++ 或 C# 的好处,我已经阅读了所有这些.我是来找 C 的.谢谢!

解决方案

对于初学者,您编写 C 代码,比如hello_world_gtk.c",然后使用适当的编译器和链接器标志编译并链接到 Gtk.这些标志由 pkg-config 工具提供给您.要获得所需的编译器标志,您可以使用以下命令调用该工具:

<前>pkg-config gtk+-2.0 --cflags

对于链接标志:

<前>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+?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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