6 Replies - 741 Views - Last Post: 19 December 2011 - 01:47 PM Rate Topic: -----

#1 shammancer  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 03-September 10

GTKMM Hello World! questions.

Posted 19 December 2011 - 12:14 PM

I discovered GTK+ and gtkmm over the weekend and I was reading over the gnome documentation. I have two questions about the Hello World! code they gave on the website.

helloworld.h
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H

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

class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

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

  //Member widgets:
  Gtk::Button m_button;
};

#endif // GTKMM_EXAMPLE_HELLOWORLD_H



main.cc
#include "helloworld.h"
#include <gtkmm/main.h>

int main (int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  HelloWorld helloworld;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(helloworld);

  return 0;
}



helloworld.cc
#include "helloworld.h"
#include <iostream>

HelloWorld::HelloWorld()
: m_button("Hello World")   // creates a new button with label "Hello World".
{
  // 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,
              &HelloWorld::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();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
  std::cout << "Hello World" << std::endl;
}



1. Why do they define GTKMM_EXAMPLE_HELLOWORLD_H
2. How is main.cc and helloworld.cc linked together?

Is This A Good Question/Topic? 0
  • +

Replies To: GTKMM Hello World! questions.

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: GTKMM Hello World! questions.

Posted 19 December 2011 - 12:26 PM

The answer to question 1 is that "GTKMM_EXAMPLE_HELLOWORLD_H" is an include guard. You can tell this by the fact that right before it is an "#ifndef", then on the next line they #define it. The answer to your second question is that they are linked together when you compile the program. So for this example, if you're using g++ and gtkmm-3.0, the command to compile it would be:
g++ helloworld.cc main.cc -o helloworld `pkg-config gtkmm-3.0 --cflags --libs`


Notice that right before the "pkg-config" is a '`', which is a backtick, not a single quote. The line also ends with another backtick, not a single quote. This is important because the backticks run the command inside of them and return the results. The command will return the location of the necessary files to compile a program that uses gtkmm 3.0. If you have a different version installed, just change the number. Also, just so you know, they show on that site how to compile the programs they have for examples.
Was This Post Helpful? 0
  • +
  • -

#3 shammancer  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 03-September 10

Re: GTKMM Hello World! questions.

Posted 19 December 2011 - 01:11 PM

What is an include guard and why would you need one?

Why do they have helloworld.cc code in a separate file compaired to in the main.cc

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: GTKMM Hello World! questions.

Posted 19 December 2011 - 01:20 PM

Sounds to me like you still need to learn some basics of C++ before you go into making GUIs. Every program needs a function named "main". Thats why the main.cc file is there. It also contains a little setup code and the code that displays the window. C++ really emphasizes data-encapsulation. To do this, you use classes. Usually you want to separate the class declaration from its actual implementation. This is done by declaring the class in a .h file and then defining the class' members in another .cc file. That way, people who are just going to use the class only need to look at the .h file and they should know how to use that class.

Onto your other question, any file can include other header files. Once a file is included into another file, it doesn't need to be included again. So to prevent this from happening, we place "include guards" in the header file. The lines that begin with a '#' are preprocessor directives. The first line "#ifndef HELLOWORLD_H" just checks to see if the macro "HELLOWORLD_H" is defined or not. If the macro is defined, the whole header file is skipped. It it isn't defined, the preprocessor moves to the next line where we define the "HELLOWORLD_H" macro. Because we started with an "#ifndef", we need to end with a "#endif". You should be able to see that the last line in the header file is exactly that. So once this header file is processed, it won't be included more than once. Hence the name, "include guards".

This post has been edited by vividexstance: 19 December 2011 - 01:21 PM

Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: GTKMM Hello World! questions.

Posted 19 December 2011 - 01:23 PM

> What is an include guard and why would you need one?

Please Google this. It's a rather basic concept and there's exceedingly huge amount of discussion on this.

> Why do they have helloworld.cc code in a separate file compaired to in the main.cc

Here's a better question. Why would you ever have more than one file? Let's say you are working on a massive application. Totally amazing. 1 million lines of code. Is it ok to have it all in one file?
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: GTKMM Hello World! questions.

Posted 19 December 2011 - 01:27 PM

I know it can be discouraging at first to write only console applications. However, by not fully knowing the language and then to try to start building a GUI application while you're still learning is NOT a good idea. If I stumble upon something I didn't know about, I will mess around with it inside a console application first. You're going to confuse yourself to the point where you might give up because there will be so much you don't understand, even the basics. Just learn more about the language while writing console applications, and when you feel you know the language well enough, then try to learn about GUIs.
Was This Post Helpful? 0
  • +
  • -

#7 shammancer  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 03-September 10

Re: GTKMM Hello World! questions.

Posted 19 December 2011 - 01:47 PM

Thank you for answering my questions. I just wanted to understand the reasoning behind splitting the file and why you would need an include guard.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1