How to Build wxWidgets 3.0 Hello World! on Ubuntu 18 with CMake
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
cd ~
mkdir hello_wxwidgets
cd hello_wxwidgets
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...\t Ctrl-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!" );
}
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} )
cd cmake-build-debug
cmake -DCMAKE_BUILD_TYPE=Debug -G " CodeBlocks - Unix Makefiles" ~ /hello_wxwidgets
cmake --build ~ /hello_wxwidgets/cmake-build-debug --target Hello -- -j 4
Very helpfull, thanks!