How to install and compile your first wxWidgets application in Visual C++ 2008 Express

I just managed to use Visual C++ 2008 Express to compile my first very simple hello world wxWidgets application.
I spent only about one hour on it, include download, setup, etc.
One hour is quite short time. I've spent a lot of hours on Qt and still have no idea how to compile it in VC 2008 Express. That's another story I will blog later.
To help you start your development on wxWidgets, here is a very simple tutorial.

Compile and install wxWidgets

1, Download wxWidgets source code from the official site.
http://www.wxwidgets.org/downloads/

I downloaded the latest stable release, 2.8.12, the wxMSW version. It's for Windows only, smaller package.

2, Extract the source code to a folder. I named the folder as wxWidgets instead of wxMSW, and no version number.

3, Fire up VC IDE. I think even VC 6 will work but not test.

4, In VC, open the project wx.dsw in folder wxWidgets\build\msw. VC 2008 will prompt to convert the project format, let it go.

5, In VC, just build the whole solution, build all projects in it. Don't feel scared if you have experimented the hours compiling time with other open source library. On my old PC, it took only several minutes to build whole wxWidgets.
Note in the solution there are several targets to choose, I choose "Unicode Debug" for debug version, will compile "Unicode Release" for release later.

6, Have some beer and wait until the compile done.

7, Now go to the folder wxWidgets\lib\vc_lib\mswud\wx, copy the file setup.h to wxWidgets\include\wx

Start your first wxWidgets project

1, Create an empty Win32 GUI project in VC.

2, Go to menu Project->[Project name] Properties, or just press Alt+F7,
In Linker->General, add the folder wxWidgets\lib\vc_lib to "Additional Library Directories".
Then select Linker->Input, in "Additional dependences", add following libraries, you need to add other wxWidgets libraries if you need more features,
Comctl32.lib
Rpcrt4.lib
wxbase28ud.lib
wxmsw28ud_adv.lib
wxmsw28ud_aui.lib
wxmsw28ud_core.lib
wxmsw28ud_richtext.lib

3, Add a new C++ source file, for example, main.cpp, and input some code and run.

Now you see your first wxWidgets.
Is it simple? YES.

Below is the simple source code I used, indeed, I copied from the tutorial on wxWidgets web site. Put it in to main.cpp you can get it run.
Note: there is no
main entry function. The line "IMPLEMENT_APP(MyApp)" is the entry. That confused me a lot before I realized that. Hope you don't get confused.

#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()

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 );
}

IMPLEMENT_APP(MyApp)

Write a comment

  • Required fields are marked with *
  • Security Code is case sensitive, 'A' is different with 'a'.

If you have trouble reading the code, click on the code itself to generate a new random code.
Security Code: