Setting up wxWidgets Environment for C++ in Ubuntu Linux

The objective here is to provide step by step instructions to setup wxWidgets for use in C++ applications under Linux.

Setup Build Environment

Ensure that the required libraries and tools for C++ development are installed:

sudo apt-get install libgtk-3-dev build-essential

sudo apt install libwxbase3.0-dev
sudo apt install libwxgtk3.0-dev
sudo apt install libwxgtk3.0-gtk3-dev

Download and Compile wxWidgets

Go to http://wxwidgets.org/downloads/ and download the link which says Source for Linux, MacOS etc. This will download a bz2 file. Unzip this file into a directory of your choice. Now we have to compile the source to form a static library which will be linked to your application when it is built.

If you have unzipped the source into a folder called wxWidgets-3.1.5 then create a new folder within it for creating the library files. You can call it anything but for now we will call it wxWidgetsLib.

mkdir wxWidgetsLib

So now you have wxWidgets-3.1.5/wxWidgetsLib

While you are in wxWidgets-3.1.5 run the following commands to build wxWidgets:

./configure --enable-shared --enable-unicode

If you want to use wxWebView control to embed a web browser then you have to add extra arguments to configure. For Ubuntu, you need to enable wxWebView and webKitGTK. But before that you need to install webkit for GTK 4.0 as below:

 sudo apt install libwebkit2gtk-4.0-dev 
./configure --enable-shared --enable-unicode --enable-webview --enable-webviewwebkit

make

At this stage the wxWidgets library should be ready for use in your applications

Testing With A Sample Program

#include <wx/string.h>
#include <wx/crt.h>

int main (int argx, char **argv) {

        wxString str1 = wxT("Linux");
        wxString str2 = wxT("Windows");
        wxString str3 = wxT("MacOS");

        wxString str = str1+ wxT(" " ) + str2 + wxT(" ") + str3;
        wxPuts(str);
}
~                                                                               
~                                                                               
~                 

Lets create a simple console based program as shown above. This can be saved as addition.cpp. We will use g++ to compile and link it into an executable:

g++ addition.cpp `wx-config --cxxflags --libs`  -o addition

Be sure to put the backticks for the arguments as shown above. They have to be backticks and not regular single quotes.

On completion you can run the program with:

./addition

The output will be as shown below:

amit@ubuntu-amit:/var/projects/c++$ ./addition
Linux Windows MacOS

What we did was create a console application using wxWidgets. Lets create a simple program which creates a default window:

window.h

#include <wx/wx.h>

class Window: public wxFrame {

  public:
	  Window(const wxString& title);
};


window.cpp

#include "window.h"

Window::Window(const wxString& title)
        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250,100)) {

        Centre();
}


main.h

#include <wx/wx.h>

class TestApp: public wxApp {

	public :
		virtual bool OnInit();
};

main.cpp

#include "main.h"
#include "window.h"

IMPLEMENT_APP(TestApp)

bool TestApp::OnInit() {

        Window *window = new Window(wxT("Test app"));
        window->Show(true);

        return true;
}


To compile and link it run:

g++ window.cpp main.cpp `wx-config --cxxflags --libs std` -o main

To run it type

./main

This will show a small resizable Window

For simple small programs g++ on the command line will suffice. But for larger projects, it is best to a make file.

2 Trackbacks / Pingbacks

  1. 1.wxWidgets – Introduction – Truelogic Blog
  2. Convert IMDB data to MySql tables using C++ – Part 1 – Truelogic Blog

Leave a Reply

Your email address will not be published.


*