在 Ubuntu c++ 上找不到 gtk/gtk.h [英] gtk/gtk.h not found on Ubuntu c++

查看:45
本文介绍了在 Ubuntu c++ 上找不到 gtk/gtk.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程比较陌生,尤其是在如何包含库和类似活动方面.过去我一直在使用 Python 进行编程,并且一直在使用 GTK 创建窗口,这也是我在使用 c++ 编程时打算做的事情.首先,这是我的代码:

I'm rather new to programming and especially when it comes to how to including libraries and alike activities. I've been programming a bit using Python in the past and I've been using GTK to create windows, something I've intended to do when programming with c++ as well. To get things started, here's my code:

#include <stdlib.h>
#include <gtk/gtk.h>

static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
  GtkWidget *dialog = NULL;

  dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

int main (int argc, char *argv[])
{
  GtkWidget *button = NULL;
  GtkWidget *win = NULL;
  GtkWidget *vbox = NULL;

  /* Initialize GTK+ */
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
  gtk_init (&argc, &argv);
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

  /* Create the main window */
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW (win), 400, 300);
  gtk_container_set_border_width (GTK_CONTAINER (win), 8);
  gtk_window_set_title (GTK_WINDOW (win), "test");
  gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
  gtk_widget_realize (win);
  g_signal_connect (win, "destroy", gtk_main_quit, NULL);

  /* Create a vertical box with buttons */
  vbox = gtk_vbox_new (TRUE, 6);
  gtk_container_add (GTK_CONTAINER (win), vbox);

  button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
  gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);

  button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
  g_signal_connect (button, "clicked", gtk_main_quit, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);

  /* Enter the main loop */
  gtk_widget_show_all (win);
  gtk_main ();
  return 0;
}

代码取自我不久前在互联网上找到的Hello world"示例.

The code was taken from a "Hello world" example I found on the internet a while back.

我知道这个问题已经得到了答案,但我的情况要复杂得多(至少从我的角度来看).首先,我已经安装了所有需要的包.顺便说一下,我运行的是 Ubuntu 14.04.

I'm aware that this issue have already gotten an answer, but my situation is much more complex (from my perspective at least). First off, I've installed all packages required. I run Ubuntu 14.04 by the way.

当我使用 g++ main.cpp 编译代码时,出现以下错误:

When I compile the code using g++ main.cpp I get the following error:

main.cpp:2:21: fatal error: gtk/gtk.h: No such file or directory
 #include <gtk/gtk.h>
                     ^
compilation terminated.

对于这个特定的错误有一个修复方法,就是像这样扩展编译命令:g++ main.cpp -I/usr/include/gtk-2.0.然而,这将提供另一个类似的错误:

There is a fix for this particular error, which is to extend the compile command like this: g++ main.cpp -I/usr/include/gtk-2.0. This however will provide another, similar error:

In file included from /usr/include/gtk-2.0/gdk/gdk.h:32:0,
                 from /usr/include/gtk-2.0/gtk/gtk.h:32,
                 from main.cpp:2:
/usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30:21: fatal error: gio/gio.h: No such file or directory
 #include <gio/gio.h>
                     ^
compilation terminated.

你也可以通过扩展这样的命令来解决这个问题(所有命令都是互联网上的,我不太明白):g++ -g -Wall -c -o program.o main.cpp -I/usr/include/gtk-2.0 $(pkg-config --libs --cflags glib-2.0).cairo.h 现在会出现错误.

You can fix this as well by extending the command like this (all commands are fund on the internet and I don't quite understand it all): g++ -g -Wall -c -o program.o main.cpp -I/usr/include/gtk-2.0 $(pkg-config --libs --cflags glib-2.0). There will now be an error with cairo.h.

如您所见,也有类似的错误.我不知道出了什么问题,但我必须相信有一个相对简单的解决方法.

As you can see there are similar errors. I have no idea what's wrong but I must believe there is a relatively easy fix.

另外,我尝试了全新安装的 Ubuntu(只需要安装软件包)并出现同样的错误.

Also, I tried a fresh install of Ubuntu (just installed packages necessary) and the same errors occur.

推荐答案

pkgconfig 命令为您提供所有必要的 -I (include) 和 -l 编译器的(链接器)语句,当包含某个包时.

The pkgconfig command gives you all of the necessary -I (include) and -l (linker) statements for the compiler, when including a certain package.

看看运行给出了什么:

pkgconfig --cflags --libs gtk+-2.0

我尝试在我的 Ubuntu 14.04.1 上编译您的代码,并且在我使用时它运行良好:

I've tried compiling your code on my Ubuntu 14.04.1 and it went fine, when I used:

g++ main.cpp `pkg-config --cflags --libs gtk+-2.0`

可能 pkg-config --libs --cflags glib-2.0 不足以提供所有必要的包含路径和共享库.

Probably pkg-config --libs --cflags glib-2.0 wasn't enough to provide all of the necessary include paths and shared libraries.

这篇关于在 Ubuntu c++ 上找不到 gtk/gtk.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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