您好stackoverflow :),
我正在编译一个基本的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 ++版本有效,所以我需要帮助。如何使链接器正常工作?
最佳答案
在examplewindow.cc文件中,您要在ExampleWindow类之外定义std::string double_to_ustring(double a)
。您需要像该类的其他所有成员方法一样对它进行限定:std::string ExampleWindow::double_to_ustring(double a)
关于c++ - gtkmm链接器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14934059/