加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:42:12 所属栏目:百科 来源:网络整理
导读:我是C和gtkmm的新手.我目前正在尝试使用窗口和按钮编译我在网上找到的教程.我在Ubuntu 12.04编译.我可以编译一个文件,但是当我尝试使用Makefile编译几个文件时,我得到一个我不明白的错误: sarah@superawesome:~/gtkexample$makeg++ -c main.ccIn file inclu
我是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

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

解决方法

您的示例中的以下include语句不正确.它的工作原理只是因为文件路径是相对于标准的/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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读