简体   繁体   English

使用 gtkmm 编译 Hello world 程序的问题

[英]issues with compiling Hello world program using gtkmm

I am new to C++ and to gtkmm.我是 C++ 和 gtkmm 的新手。 I am currently trying to compile a tutorial I found online using a window and a button.我目前正在尝试使用窗口和按钮编译我在网上找到的教程。 I am compiling in Ubuntu 12.04.我正在 Ubuntu 12.04 中编译。 I can compile a single file fine but when I try to compile several files using a Makefile I get an error that I don't understand:我可以很好地编译单个文件,但是当我尝试使用 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

I really don't understand the error, I've been searching for hours.我真的不明白这个错误,我已经搜索了几个小时。 I would really appreciate any help or insight into my problem.我真的很感激对我的问题的任何帮助或见解。

These are my 3 files and Makefile:这些是我的 3 个文件和 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);
}

and HelloSarah.cc和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;
}

and finally my Makefile:最后是我的 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

The following include statement in your example is not correct.您示例中的以下包含语句不正确。 It works only because the file path is relative to the standard /usr/include/ directory, but the include statement in button.h does not, so you get an error message.它之所以有效,只是因为文件路径是相对于标准的/usr/include/目录,而button.h中的 include 语句却无效,因此您会收到一条错误消息。

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

You have to tell g++ where the necessary include files and shared objects can be found.您必须告诉g++在哪里可以找到必要的包含文件和共享对象。 You can use the output of pkg-config to do that job.您可以使用pkg-config的输出来完成这项工作。

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

The whole g++ command should be something like that.整个g++命令应该是这样的。

g++ `pkg-config --cflags --libs gtkmm-3.0` -c HelloSarah.cc

After that you can simply use the include line in gtkmm Hello World .之后,您可以简单地使用 gtkmm Hello World 中的 include 行。

#include <gtkmm/button.h>

I had this problem too on Ubuntu .我在Ubuntu也有这个问题。

The solution:解决方案:

sudo apt-get install libgtkmm-3.0-dev

You can use any version as you need.您可以根据需要使用任何版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM