0

I'm practicing with gtk (or gtkmm in this case), to which I'm completely new and I'm relatively new to c++. I got a working program that could open a window and put a few widgets in it, but now I'm trying to add an action to a button, and it just won't work.

main.cc:

    #include <iostream>
    #include "buttons.h"
    #include <gtkmm/application.h>

    void printLine()
    {
        std::cout<<"you pressed the button"<<std::endl;
    }

    int main(int argc, char *argv[])
    {
        Glib::RefPtr<Gtk::Application> app =
        Gtk::Application::create(argc, argv,
        "org.gtkmm.examples.base");

        Buttons buttons;


         return app->run(buttons);

    }

buttons.h:

    #ifndef GTKMM_EXAMPLE_BUTTONS_H
    #define GTKMM_EXAMPLE_BUTTONS_H

    #include <gtkmm/window.h>
    #include <gtkmm/button.h>
    #include <gtkmm/box.h>

    class Buttons : public Gtk::Window
    {
    public:
        Buttons();

        virtual ~Buttons();

    protected:
        //Signal handlers:
        void on_button_clicked();

        //Child widgets:
        Gtk::Button m_button;
        Gtk::Box buttonBox;
    };

    #endif //GTKMM_EXAMPLE_BUTTONS_H 

buttons.cc:

    #include <iostream>
    #include "buttons.h"

    Buttons::Buttons()
    {
    m_button.add_pixlabel("info.xpm", "click here");

    set_title("Pixmap'd buttons!");
    set_border_width(10);

    m_button.signal_clicked().connect( sigc::mem_fun(*this,
          &Buttons::on_button_clicked) );

    add(buttonBox);

    buttonBox.pack_start(m_button);

    //m_button.show();
   show_all_children();
   }

   Buttons::~Buttons()
   {

   } 

   void Buttons::on_button_clicked()
   {
   printLine();
   }

I am using g++ to compile the program and it gives me this error message: g++ main.cc -o button pkg-config gtkmm-3.0 --cflags --libs /tmp/ccKyphYe.o: In function main': main.cc:(.text+0x93): undefined reference toButtons::Buttons()' main.cc:(.text+0xc5): undefined reference to Buttons::~Buttons()' main.cc:(.text+0x124): undefined reference toButtons::~Buttons()' collect2: error: ld returned 1 exit status

  • you have to put all your source files in the compile line, so just add buttons.cc right after main.cc and you should be good. There are other ways to do it, but just to get you going, that should work. – xaxxon May 20 '16 at 23:30

1 Answers1

1

You have to put all your source files in the compile line, so just add buttons.cc right after main.cc and you should be good. There are other ways to do it, but just to get you going, that should work.

The longer answer is that the compiler compiles each src file (.cc files in your example) separately and builds object files (.o or .obj). To do this, all it needs are the declarations of the things it uses (#include'd in header files). If they are missing, you get a "compiler error".

But later when it actually puts together the final program that you are going to run, it needs the actual definitions (the actual code) for everything that is used, and if it can't find the actual definition, you get "undefined reference" errors. This is called a "linker error". This means you are missing libraries, archives, or object (.obj) files.

HOWEVER, when you put everything on the same compiler line -- all your c++ src files including one with a main() function, the compiler automatically generates the object files and does the linking all in one step.

xaxxon
  • 19,189
  • 5
  • 50
  • 80