簡體   English   中英

使用 gtkmm 編譯 Hello world 程序的問題

[英]issues with compiling Hello world program using gtkmm

我是 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

我真的不明白這個錯誤,我已經搜索了幾個小時。 我真的很感激對我的問題的任何幫助或見解。

這些是我的 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 

主文件

#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

您示例中的以下包含語句不正確。 它之所以有效,只是因為文件路徑是相對於標准的/usr/include/目錄,而button.h中的 include 語句卻無效,因此您會收到一條錯誤消息。

#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 Hello World 中的 include 行。

#include <gtkmm/button.h>

我在Ubuntu也有這個問題。

解決方案:

sudo apt-get install libgtkmm-3.0-dev

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM