gtkmm 链接器错误

Posted

技术标签:

【中文标题】gtkmm 链接器错误【英文标题】:gtkmm linker error 【发布时间】:2013-02-18 10:25:44 【问题描述】:

你好 ***:), 我正在编译一个基本的 gui 程序,它需要 2 个双精度数的总和并将它们打印出来。我的代码其实很简单,先贴出我命令行编译的3个文件

"g++ main.cc examplewindow.cc -o main `pkg-config gtkmm-3.0 --cflags --libs`"

命令行的错误是:

/tmp/ccSeXudb.o: In function `ExampleWindow::on_button_clicked_calculate()': examplewindow.cc:(.text+0x1e70): undefined reference to `ExampleWindow::double_to_ustring(double)'

collect2: error: ld returned 1 exit status

这些是 3 个文件:

                                    examplewindow.h

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>
#include <boost/lexical_cast.hpp>
#include <stdlib.h>
#include <string>

class ExampleWindow : public Gtk::Window

public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  void on_checkbox_editable_toggled();
  void on_checkbox_visibility_toggled();
  Glib::ustring get_entry1();
  Glib::ustring get_entry2();
  std::string double_to_ustring(double a);
  void on_button_clicked_calculate();
  void on_button_close();

  //Child widgets:
  //Gtk::Box m_HBox;
  Gtk::Box m_VBox;
  Gtk::Box m_ZBox;
  Gtk::Entry m_Entry, m_Entry2, m_Entry3; 
  Gtk::Button m_Button_Close, m_Button_Calculate;
  //Gtk::CheckButton m_CheckButton_Editable, m_CheckButton_Visible;
  Gtk::Label m_Label_Sum, m_Label_Equals;
;

#endif //GTKMM_EXAMPLEWINDOW_H





                                     main.cc

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

int main(int argc, char *argv[])

  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv,     "org.gtkmm.example");

  ExampleWindow window;

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





                                  examplewindow.cc

#include "examplewindow.h"
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <stdlib.h>
#include <string>


ExampleWindow::ExampleWindow()
: m_VBox(Gtk::ORIENTATION_VERTICAL),
  m_ZBox(Gtk::ORIENTATION_HORIZONTAL),
  m_Button_Calculate("Calculate"),
  m_Button_Close("Close")

  set_size_request(200, 100);
  set_title("Trial Version:P");

  add(m_VBox);


  m_VBox.add(m_ZBox);

  m_Entry.set_max_length(50);
  m_Entry.set_text("First");
  m_Entry.set_text(m_Entry.get_text() + " value");
  m_Entry.select_region(0, m_Entry.get_text_length());
  m_ZBox.pack_start(m_Entry);

  m_Label_Sum.set_text("+");
  m_ZBox.pack_start(m_Label_Sum); 

  m_Entry2.set_max_length(50);
  m_Entry2.set_text("Second");
  m_Entry2.set_text(m_Entry2.get_text() + " value");
  m_Entry2.select_region(0, m_Entry2.get_text_length());
  m_ZBox.pack_start(m_Entry2);

  m_Label_Equals.set_text("=");
  m_ZBox.pack_start(m_Label_Equals);

  m_Entry3.set_max_length(50);
  m_Entry3.set_text("The");
  m_Entry3.set_text(m_Entry3.get_text() + " result"); 
  m_Entry3.set_editable(0); 
  m_Entry3.select_region(0, m_Entry3.get_text_length());
  m_ZBox.pack_start(m_Entry3);


  m_Button_Calculate.signal_clicked().connect( sigc::mem_fun(*this,     &ExampleWindow::on_button_clicked_calculate) );
  m_VBox.pack_start(m_Button_Calculate);
  m_Button_Calculate.set_can_default();
  m_Button_Calculate.grab_default();
  m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_button_close) );
  m_VBox.pack_start(m_Button_Close);
  m_Button_Close.set_can_default();
  m_Button_Close.grab_default();

  show_all_children();


ExampleWindow::~ExampleWindow()



Glib::ustring ExampleWindow::get_entry1()

  return m_Entry.get_text();


Glib::ustring ExampleWindow::get_entry2()

  return m_Entry2.get_text();

std::string double_to_ustring(double a)

  std::string str = boost::lexical_cast<std::string>(a);
  return str; 


void ExampleWindow::on_button_clicked_calculate()
 
  Glib::ustring a = get_entry1();
  Glib::ustring b = get_entry2();
  double m = std::atof( a.c_str() ) + std::atof( b.c_str() );
  Glib::ustring z = double_to_ustring(m);
  m_Entry3.set_text(z);


void ExampleWindow::on_button_close()

  hide();


我已经制作了一个只有 c++ 的版本:

#include <iostream>
#include <boost/lexical_cast.hpp>
#include <stdlib.h>
#include <string>

using namespace std;

string double_to_string(double a)

  string str = boost::lexical_cast<std::string>(a);
  return str; 


int main()

  string a="3", b="5";
  double m = atof( a.c_str() ) + atof( b.c_str() );
  string z = double_to_string(m);
  cout << z << endl;

c++ 版本有效,所以我需要帮助。如何让链接器工作?

【问题讨论】:

【参考方案1】:

在您的 examplewindow.cc 文件中,您正在从 ExampleWindow 类中定义 std::string double_to_ustring(double a)。你需要像类的所有其他成员方法一样限定它:std::string ExampleWindow::double_to_ustring(double a)

【讨论】:

以上是关于gtkmm 链接器错误的主要内容,如果未能解决你的问题,请参考以下文章

为啥链接器链接了错误的函数?

奇怪的链接器错误

GLEW 链接器错误

RestKit CocoaPods 链接器错误

VC++ 6 链接器错误

链接器错误 - 不确定它没有链接