Tuesday, February 22, 2011

Setup wxWidgets in Codeblocks

wxWidgets is free software, multi-platform GUI library written in C++ programing language. GUI applications created using the wxWidgets library run on multiple platforms such as Windows, Linux, Mac, and others that utilize the native platform's controls and utilities. Since it is built and compiled in C++, it has high performance and is nearly as fast as using the native toolkits themselves.

The Codeblocks IDE that we are using is also developed using wxWidgets. Here, I will guide you through a step-by-step procedure to set up wxWidgets in Codeblocks IDE on Windows.

Download Library Files:

Open the wxWidgets download page https://www.wxwidgets.org/downloads/ and download Windows Binaries. You can download source code and compile library using Codeblocks or visual C++ compiler but it is a very complex process and difficult for the beginner. So, I will help you to setup a library using pre-compiled library and header files. In the download pages under the Binaries section, there is "Download Windows Binaries" button. Click this button and select "MinGW-w64 8.1" option (Note: if you are using an older version of Codeblocks then you should choose as per your MinGW version. However, I suggest you to use the latest version of the Codeblocks and download the latest version of the MinGW library). Now download "Header Files" and "Development Files" 32-Bit or 64-Bit as per the Codeblocks that you are using. I am using 64 Codeblocks so I will download 64-Bit version "Development Files".  

Steps to setup wxWidgets library:
  1. Extract downloaded zip files both "Header" and "Development Files". (Note: you may need 7zip to extract files)
  2. Create a folder in any directory and give the name "wxWidgets" (eg. c:\wxWidgets).
  3. Browse extracted files in step 1. Copy "include" folder from the downloaded Header and paste it inside the directory that you have created in step 2. Copy "lib"  folder from the downloaded Development Files and paste it inside the directory that you have created in step2. If you open "lib" folder you will find dll files, you can copy all dll files to "C:\Windows\System32" folder if you are using 32-bit or "C:\Windows\SysWOW64" folder if you are using 64-bit. However, I do not suggest you to copy dll files to the system folder in the development phase of an application.
  4. Now, open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
  6. Go to the menu bar and select Project-> Properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Make sure that in left side panel you must select "<project name>" and under the "Compiler" tab add the path of wxWidgets include folder (e.g. c:\wxWidgets\include) and under "Linker" tab add the path of wxWidgets lib folder (e.g. c:\wxWidgets\lib\gcc810_x64_dll) where you can find *.a and *.dll files.
  7. Select "Debug" option in left side panel and click the "Search directories" tab. Under the "Compiler" tab add the path of folder "mswud". This folder is located inside wxWidgets lib folder (e.g. c:\wxWidgets\lib\gcc810_x64_dll\mswud). Note: if you are using 32-bit then the path might be different (e.g. c:\wxWidgets\lib\gcc_dll\mswud). Now select "Linker Setting" tab and add two linkers wxmsw31ud_core and wxbase31ud. Please type exactly the same name don't write "lib" prefix and ".a" extension.
  8. Select "Release" option in left side panel and click the "Search directories" tab. Under the "Compiler" tab add the path of folder "mswu". This folder is located inside wxWidgets lib folder (e.g. c:\wxWidgets\lib\gcc810_x64_dll\mswu). Note: if you are using 32-bit then the path might be different (e.g. c:\wxWidgets\lib\gcc_dll\mswu). Now select "Linker Setting" tab and add two linkers wxmsw31u_core and wxbase31u. Please type exactly the same name don't write "lib" prefix and ".a" extension.  
Please watch the video below for a detailed explanation.



Now copy the sample code given below inside "main.cpp" file and build and run it. Sample code is taken from wxWidgets official website.

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include &lt"wx/wxprec.h"&gt
#ifndef WX_PRECOMP
    #include &lt"wx/wx.h"&gt
#endif
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
    MyFrame();
private:
    void OnHello(wxCommandEvent& event);
    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
};
enum
{
    ID_Hello = 1
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;
}
MyFrame::MyFrame()
    : wxFrame(NULL, wxID_ANY, "Hello World")
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                  "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
    Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}

No comments:

Post a Comment