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

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

问题描述

我对编程相当陌生,特别是在涉及到图书馆和类似活动时。过去我一直在使用Python进行编程,我一直在使用GTK创建窗口,这也是我在使用c ++进行编程时所​​要做的。为了让事情开始,下面是我的代码:

  #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;
$ b $ * *初始化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);

/ *创建主窗口* /
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);

/ *创建一个带按钮的垂直框* /
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);

/ *输入主循环* /
gtk_widget_show_all(win);
gtk_main();
返回0;
}

代码取自我在互联网上找到的Hello world示例一会儿回来。



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



使用 g ++ main.cpp 编译代码时,我得到以下错误:

  main.cpp:2:21:致命错误:gtk / gtk.h:没有这样的文件或目录
#include< gtk / gtk.h>
^
编译终止。

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

 包含在/usr/include/gtk-2.0/gdk/gdk的文件中。 h:32:0,
来自/usr/include/gtk-2.0/gtk/gtk.h:32,
来自main.cpp:2:
/ usr / include / gtk- 2.0 / gdk / gdkapplaunchcontext.h:30:21:致命错误:gio / gio.h:没有这样的文件或目录
#include< gio / gio.h>
^
编译终止。

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



正如你所看到的,有类似的错误。我不知道有什么问题,但我必须相信有一个相对简单的解决方法。

另外,我尝试了全新安装的Ubuntu(只安装了必要的软件包)和相同的错误发生。

解决方案 pkgconfig 命令为您提供所有必要的 -I (包括)和 -l <​​/ code>(链接器)语句在包含某个包时编译器。 p>

看一下跑步给出的内容:

  pkgconfig --cflags --libs gtk + -2.0 

我试过在Ubuntu 14.04.1上编译你的代码,当我使用时,情况良好:

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

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


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

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

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.

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.

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.

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.

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

解决方案

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

Take a look what is given by running:

pkgconfig --cflags --libs gtk+-2.0

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`

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天全站免登陆