使用gtkmm编译 Hello world 程序出现问题

3
我是一名新手,对C++和gtkmm都不熟悉。我正在尝试使用一个窗口和一个按钮来编译我在网上找到的教程。我的操作系统是Ubuntu 12.04。我可以成功编译单个文件,但当我尝试使用Makefile编译多个文件时,出现了一个我不理解的错误:
sarah@superawesome:~/gtkexample$ make
g++ -c main.cc
In file included from HelloSarah.h:4:0,
                 from main.cc:1:
/usr/include/gtkmm-3.0/gtkmm/button.h:7:28: fatal error: glibmm/ustring.h: No such file or directory
compilation terminated.
make: *** [main.o] Error 1

我真的不理解这个错误,我已经搜索了几个小时了。如果有人能帮忙或提供任何见解来解决我的问题,我将非常感激。

以下是我的三个文件和Makefile:

#ifndef GTKMM_HELLOSARAH_H
#define GTKMM_HELLOSARAH_H

#include <gtkmm-3.0/gtkmm/button.h>
#include <gtkmm/window.h>

class HelloSarah : public Gtk::Window
{

public:
  HelloSarah();
  virtual ~HelloSarah();

protected:
  //Signal handlers:
  void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
};

#endif 

and

main.cc

#include "HelloSarah.h"
#include <gtkmm/application.h>

int main (int argc, char *argv[])
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv,     "org.gtkmm.example");

  HelloSarah hellosarah;

  //Shows the window and returns when it is closed.
  return app->run(hellosarah);
}

以及HelloSarah.cc

#include "helloSarah.h"
#include <iostream>

HelloSarah::HelloSarah()
: m_button("Hello Sarah")   // creates a new button with label "HelloSarah".
{
  // Sets the border width of the window.
  set_border_width(10);

  // When the button receives the "clicked" signal, it will call the
  // on_button_clicked() method defined below.
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
          &HelloSarah::on_button_clicked));

  // This packs the button into the Window (a container).
  add(m_button);

  // The final step is to display this newly created widget...
  m_button.show();
}

HelloSarah::~HelloSarah()
{
}

void HelloSarah::on_button_clicked()
{
  std::cout << "Hello Sarah" << std::endl;
}

最后是我的Makefile:

app:            main.o HelloSarah.o
                g++ -o app main.o HelloSarah.o

main.o:         main.cc HelloSarah.h
            g++ -c main.cc

HelloSarah.o:   HelloSarah.cc HelloSarah.h
            g++ -c HelloSarah.cc

clean:      
            rm -f *.o app

3
请尝试运行以下命令:'sudo apt-get install libglibmm-2.4-dev'。 - Guy Sirton
2个回答

9
您的示例中以下的包含语句不正确。它之所以能够工作只是因为文件路径相对于标准的/usr/include/目录,但是在button.h中的包含语句却不是这样,因此导致了错误信息的出现。
#include <gtkmm-3.0/gtkmm/button.h>

您需要告诉 g++ 编译器必要的包含文件和共享对象的位置。您可以使用 pkg-config 的输出来完成这项工作。

pkg-config --cflags --libs gtkmm-3.0

整个g++命令应该像这样。
g++ `pkg-config --cflags --libs gtkmm-3.0` -c HelloSarah.cc

之后,您可以在gtkmm中简单地使用include行Hello World

#include <gtkmm/button.h>

你忘记给 g++ 添加 -Wall -g 参数了。 - Basile Starynkevitch

3
我也在Ubuntu上遇到了这个问题。
解决方案:
sudo apt-get install libgtkmm-3.0-dev

您可以根据需要使用任何版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接