Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on March 26, 2011, 07:10:42 am

Title: Installing wxWidgets and compiling software
Post by: Munchor on March 26, 2011, 07:10:42 am
Last night, I installed wxWidgets 2.9.1 on my Ubuntu 10.04. It all went well, and it works:

Code: [Select]
david@DavidPC:~$ wx-config --version
2.9.1
david@DavidPC:~$

It was installed. Now I want to make my first GUI program, and used this code:

Code: [Select]
/*
 * hworld.cpp
 */

#include "wx/wx.h"

class MyApp: public wxApp
{
    virtual bool OnInit();
};

class MyFrame: public wxFrame
{
public:

    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

    DECLARE_EVENT_TABLE()
};

enum
{
    ID_Quit = 1,
    ID_About,
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
                                  wxSize(450,340) );
    frame->Show(true);
    SetTopWindow(frame);
    return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
    wxMenu *menuFile = new wxMenu;

    menuFile->Append( ID_About, _("&About...") );
    menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, _("E&xit") );

    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, _("&File") );

    SetMenuBar( menuBar );

    CreateStatusBar();
    SetStatusText( _("Welcome to wxWidgets!") );
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    Close(TRUE);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox( _("This is a wxWidgets Hello world sample"),
                  _("About Hello World"),
                  wxOK | wxICON_INFORMATION, this);
}

My question being, how to compile it?

When I go to the console and type g++ helloWorld.cpp I get:

Code: [Select]
g++ helloWorldGUI.cpp
helloWorldGUI.cpp:5:20: error: wx/wx.h: Ficheiro ou directoria inexistente
helloWorldGUI.cpp:8: error: expected class-name before ‘{’ token
helloWorldGUI.cpp:13: error: expected class-name before ‘{’ token
helloWorldGUI.cpp:16: error: ISO C++ forbids declaration of ‘wxString’ with no type
helloWorldGUI.cpp:16: error: expected ‘,’ or ‘...’ before ‘&’ token
helloWorldGUI.cpp:18: error: ‘wxCommandEvent’ has not been declared
helloWorldGUI.cpp:19: error: ‘wxCommandEvent’ has not been declared
helloWorldGUI.cpp:21: error: ISO C++ forbids declaration of ‘DECLARE_EVENT_TABLE’ with no type
helloWorldGUI.cpp:22: error: expected ‘;’ before ‘}’ token
helloWorldGUI.cpp:22: error: expected ‘;’ before ‘}’ token
helloWorldGUI.cpp:30: error: ‘wxFrame’ has not been declared
helloWorldGUI.cpp:31: error: expected constructor, destructor, or type conversion before ‘EVT_MENU’
helloWorldGUI.cpp:46: error: ISO C++ forbids declaration of ‘wxString’ with no type
helloWorldGUI.cpp:46: error: expected ‘,’ or ‘...’ before ‘&’ token
helloWorldGUI.cpp: In constructor ‘MyFrame::MyFrame(int)’:
helloWorldGUI.cpp:47: error: class ‘MyFrame’ does not have any field named ‘wxFrame’
helloWorldGUI.cpp:47: error: ‘NULL’ was not declared in this scope
helloWorldGUI.cpp:47: error: ‘title’ was not declared in this scope
helloWorldGUI.cpp:47: error: ‘pos’ was not declared in this scope
helloWorldGUI.cpp:47: error: ‘size’ was not declared in this scope
helloWorldGUI.cpp:49: error: ‘wxMenu’ was not declared in this scope
helloWorldGUI.cpp:49: error: ‘menuFile’ was not declared in this scope
helloWorldGUI.cpp:49: error: expected type-specifier before ‘wxMenu’
helloWorldGUI.cpp:49: error: expected ‘;’ before ‘wxMenu’
helloWorldGUI.cpp:51: error: ‘_’ was not declared in this scope
helloWorldGUI.cpp:55: error: ‘wxMenuBar’ was not declared in this scope
helloWorldGUI.cpp:55: error: ‘menuBar’ was not declared in this scope
helloWorldGUI.cpp:55: error: expected type-specifier before ‘wxMenuBar’
helloWorldGUI.cpp:55: error: expected ‘;’ before ‘wxMenuBar’
helloWorldGUI.cpp:58: error: ‘SetMenuBar’ was not declared in this scope
helloWorldGUI.cpp:60: error: ‘CreateStatusBar’ was not declared in this scope
helloWorldGUI.cpp:61: error: ‘SetStatusText’ was not declared in this scope
helloWorldGUI.cpp: At global scope:
helloWorldGUI.cpp:64: error: variable or field ‘OnQuit’ declared void
helloWorldGUI.cpp:64: error: ‘wxCommandEvent’ was not declared in this scope
helloWorldGUI.cpp:64: error: ‘event’ was not declared in this scope
helloWorldGUI.cpp:64: error: ‘WXUNUSED’ was not declared in this scope
helloWorldGUI.cpp:69: error: variable or field ‘OnAbout’ declared void
helloWorldGUI.cpp:69: error: ‘wxCommandEvent’ was not declared in this scope
helloWorldGUI.cpp:69: error: ‘event’ was not declared in this scope
helloWorldGUI.cpp:69: error: ‘WXUNUSED’ was not declared in this scope
david@DavidPC:~/Documentos/C++$

No idea why though :(
Title: Re: Installing wxWidgets and compiling software
Post by: jnesselr on March 26, 2011, 11:42:42 am
well, wx/wx.h isn't found, so it's freaking out about every thing that even includes it.  So find that file on your hard drive, and make sure that the compiler knows where it is.  (Possibly includes adding the directory to your PATH, which means I need to know what OS you are on.)
Title: Re: Installing wxWidgets and compiling software
Post by: christop on March 26, 2011, 01:21:04 pm
Like graphmastur said, you need to tell the compiler where the wx headers are. That's actually pretty easy since it looks like you're using a Linux system. To do this, run the following commands:
Code: [Select]
g++ -c `wx-config --cxxflags` helloWorldGUI.cpp
g++ -o helloWorldGUI helloWorldGUI.o `wx-config --libs`

The first command compiles your source file helloWorldGUI.cpp and creates the object file helloWorldGUI.o. The "wx-config --cxxflags" command tells g++ where the wx headers are.

The second command links the object file helloWorldGUI.o into an executable file named helloWorldGUI. The "wx-config --libs" command tells g++ where the wx libraries are.

Both of the wx-config commands above print out the appropriate flags for the compiler/linker (g++), and they are put inside backticks `` so their output is inserted directly into the g++ command lines.

EDIT: jimbauwens makes a good point in the post after mine. Make sure you have the dev or devel version installed, and then you can run the commands above.
Title: Re: Installing wxWidgets and compiling software
Post by: Jim Bauwens on March 26, 2011, 01:21:52 pm
Scout, did you install wx using the package manager?

If so, you probably installed only the compiled libraries, and not the development libs.
The latest  wx dev version in the package manager is 2.8, so I don't know if it will work with what you already have installed. But you can always try.

To install all the wx development libs:
Code: [Select]
sudo apt-get install libwxgtk2.8-dev
Now try compiling it again.
Hope it works. :)
Title: Re: Installing wxWidgets and compiling software
Post by: Munchor on March 27, 2011, 09:21:51 am
I tried christop's code too (as said in the tutorial) but I get the same error as if I tried 'g++ hworld.cpp', hence I thought it didn't make a difference. I will try other codes later :)
Title: Re: Installing wxWidgets and compiling software
Post by: jnesselr on March 27, 2011, 04:38:20 pm
You still didn't say what OS you have. :D
Title: Re: Installing wxWidgets and compiling software
Post by: Jim Bauwens on March 28, 2011, 03:11:41 am
He did, in his first post : Ubuntu 10.04 :)
Title: Re: Installing wxWidgets and compiling software
Post by: jnesselr on March 28, 2011, 07:42:55 am
Excellent, well he still didn't tell me when I get new glasses...

Anyway, yeah, it still can't find wx/wx.h, so that's your problem.
Title: Re: Installing wxWidgets and compiling software
Post by: Munchor on March 28, 2011, 07:43:36 am
I finally did it, and it's working well =D Thanks.
Title: Re: Installing wxWidgets and compiling software
Post by: jnesselr on March 28, 2011, 07:44:26 am
I finally did it, and it's working well =D Thanks.
Excellent.  Do you mind sharing how you got it to work?
Title: Re: Installing wxWidgets and compiling software
Post by: Munchor on March 28, 2011, 07:51:03 am
Code: [Select]
david@DavidPC:~/Documentos/C++$ g++ -c `wx-config --cxxflags` helloWorldGUI.cpp
david@DavidPC:~/Documentos/C++$ g++ -o helloWorldGUI helloWorldGUI.o `wx-config --libs`
david@DavidPC:~/Documentos/C++$ ./helloWorldGUI