Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patrickmoffitt/9f156d305755f6b5842eac2e8851fd90 to your computer and use it in GitHub Desktop.
Save patrickmoffitt/9f156d305755f6b5842eac2e8851fd90 to your computer and use it in GitHub Desktop.
Step-by-step instructions for installing wxWidgets 3.0 and CMake on Ubuntu 18 for the purpose of building and running the Hello World! demo application.

How to Build wxWidgets 3.0 Hello World! on Ubuntu 18 with CMake

Install wxWidgets 3.0

sudo apt-get update
sudo apt-get full upgrade
sudo apt install wx-common wx3.0-doc wx3.0-examples wx3.0-headers wx3.0-i18n \
cmake libwxbase3.0-dev libwxbase3.0-dev libwxgtk-media3.0-dev \
libwxgtk-media3.0-gtk3-dev libwxgtk-webview3.0-gtk3-dev libwxgtk3.0-dev \
libwxgtk3.0-gtk3-dev libwxsqlite3-3.0-dev libcanberra-gtk-dev \
libcanberra-gtk-module

Hello World!

  1. cd ~
  2. mkdir hello_wxwidgets
  3. cd hello_wxwidgets
  4. Create a file named main.cpp containing:
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifndef WX_PRECOMP
  #include <wx/wx.h>
#endif

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

class MyFrame : public wxFrame {
public:
    MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size);

private:
    void OnHello(wxCommandEvent &event);

    void OnExit(wxCommandEvent &event);

    void OnAbout(wxCommandEvent &event);

    wxDECLARE_EVENT_TABLE();
};

enum {
    ID_Hello = 1
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
                EVT_MENU(ID_Hello,   MyFrame::OnHello)
                EVT_MENU(wxID_EXIT,  MyFrame::OnExit)
                EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()

wxIMPLEMENT_APP(MyApp);

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

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
        : wxFrame(NULL, wxID_ANY, title, pos, size) {
    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!" );
}

void MyFrame::OnExit(wxCommandEvent &event) {
    Close(true);
}

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

void MyFrame::OnHello(wxCommandEvent &event) {
    wxLogMessage("Hello world from wxWidgets!");
}

Configure CMake

  1. Create a file name CMakeLists.txt containing:
cmake_minimum_required(VERSION 3.10)
project(Hello_WxWidgets)

set(CMAKE_VERBOSE_MAKEFILE  ON)
set(CMAKE_FIND_DEBUG_MODE ON)

set(CMAKE_CXX_STANDARD 17)

add_executable(Hello main.cpp)

set(wxWidgets_USE_DEBUG ON)
set(wxWidgets_USE_UNICODE ON)
find_package(wxWidgets REQUIRED net gl core base)
include(${wxWidgets_USE_FILE})
target_link_libraries(Hello ${wxWidgets_LIBRARIES})

Generate the Makefiles

cd cmake-build-debug
cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" ~/hello_wxwidgets

Build Hello wxWidgets

cmake --build ~/hello_wxwidgets/cmake-build-debug --target Hello -- -j 4

Run Hello wxWidgets

./Hello
@RedSnowStudioOfficial
Copy link

Very helpfull, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment