如何在C文件中包含gtk / gtk.h [英] How to include gtk/gtk.h in a C file

查看:167
本文介绍了如何在C文件中包含gtk / gtk.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C开发很新,想用GTK创建一个GUI。我已经下载并安装了gtk 3.6.4包。我试图编译一些这样的代码:

  #include< 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();
返回0;
}

我真的很难理解如何包含头文件。我见过一些讨论使用pkg-config命令的线程:

  pkg-config --cflags --libs gtk + -3.0 

应该在哪里插入这段代码?它在命令窗口上运行并生成一些输出,但这并不能解决我在代码中的MS Visual Studio中遇到的错误。我提前道歉重复一个问题,但我仍然无法理解这个pkg-config代码应该在哪里运行,而且我不清楚其他响应。

pkg-config 行是一个shell命令,它在标准输出上产生你需要传递以使用该函数的编译器标志你要。例如,

  pkg-config --cflags gtk + -3.0 

为我生成

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

你想要做的是使用你的shell的一个特性,在这个特性中,一个命令的输出被放到另一个命令的命令行中。在bash中,这是通过`...`完成的:

  gcc -o program program.c`pkg-config --cflags --libs gtk + -3.0` 

对于bash,另一种语法是 $(...)

  gcc -o program program.c $(pkg-config --cflags --libs gtk + -3.0)

对于GNU make makefile,如果你想使用 $(...),你将需要使用 shell function:

  gcc -o program program.c $(shell pkg-config --cflags --libs gtk + -3.0) 

- cflags 产生C编译器标志, - libs 会产生链接器标志。如果您使用makefile进行构建,则只需向产生 .o - cflags $ c>文件,并仅向产生最终可执行文件的配方提供 - libs 。要使用单个命令将单个C文件直接编译成可执行文件,请同时提供。


Im very new to development in C and would like to create a GUI using GTK. Ive already downloaded and installed gtk 3.6.4 bundle. Im trying to compile some example code like this:

#include <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;
}

I'm really having trouble understanding how to include the header files. I've seen a couple threads which discuss using a pkg-config command like :

pkg-config --cflags --libs gtk+-3.0

Where should this code be inserted? It runs on the command window and generates some output but this doesnt solve the error I get in MS visual studio in my code. I apologize in advance for repeating a question but I still am having trouble understanding where this pkg-config code should be run and that is not clear to me from the other responses.

解决方案

The pkg-config line is a shell command that produces on standard output the compiler flags you would need to pass to use the function you want. For example,

pkg-config --cflags gtk+-3.0

produces for me

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

What you want to do is use a feature of your shell in which the output of a command is dropped into the command line of another one. In bash, this is done with `...`:

gcc -o program program.c `pkg-config --cflags --libs gtk+-3.0`

For bash, another syntax is $(...):

gcc -o program program.c $(pkg-config --cflags --libs gtk+-3.0)

For GNU make makefiles, if you want to use $(...), you will need to use the shell function:

gcc -o program program.c $(shell pkg-config --cflags --libs gtk+-3.0)

--cflags produces the C compiler flags, --libs produces the linker flags. If you're building using a makefile, you'll want to only provide --cflags to the recipe that produces the .o files and only provide --libs to the recipe that produces the final executable. For compiling a single C file directly into an executable with a single command, provide both.

这篇关于如何在C文件中包含gtk / gtk.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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