Introduction to wxWidgets

Contents

Getting Started with wxWidgets

wxWidgets is a leightweight GUI toolkit that is primarily targeted for C++ but is also available for other languages including Python. It gives applications a truly native look by using each platform's native API. wxWidgets is completely open source using the wxWidgets licence which is a more liberal version of the LGPL (allowing distribution without the source code). It is very actively developed and maintained and has good support for all common programming environments. The source code is available on the project's Github page. Reference applications built with wxWidgets include Audacity and Code::Blocks.

To get started, install the required dependencies

sudo apt install libwxgtk3.2-dev

After the installation, follow the official Hello World Example. Save your code to hello_wx.cpp To compile the example, type

clang++ hello_wx.cpp `wx-config --cxxflags --libs` -o hello_wx

Start your first GUI application by typing ./hello_wx.

wxWidgets hello world application

Compiling wxWidgets Programs with GNU Make

wxWidgets files can, of course, also be compiled with GNU Make. Use the source below as an example

# Sample Makefile for wx applications
WX_CONFIG := wx-config
WX_CXXFLAGS := $(shell $(WX_CONFIG) --cxxflags)
WX_LIBS := $(shell $(WX_CONFIG) --libs)

APPLICATION := hello_wx  # adjust name to your desired application name
OBJECTS := hello_wx.o app.o  # enter the .o files or generate this list

all: $(APPLICATION)

$(APPLICATION): $(OBJECTS)
	$(CXX) -o $@ $(OBJECTS) $(LDFLAGS) $(WX_LIBS) $(LIBS)

$(OBJECTS): %.o: %.cpp
	$(CXX) -c -o $@ $(WX_CXXFLAGS) $(CXXFLAGS) $<

.PHONY: clean
clean:
	find . -name '*~' -o -name '*.o' -o -name $(APPLICATION) | xargs rm

Download the above template Makefile.

Compiling wxWidgets Programs with CMake

While GNU Make is a great build system for learning and understanding how build files work, Makefiles quickly get complicated and have portability issues between operating systems. Hence, I recommend using CMake for generating the build files for make or ninja.

cmake_minimum_required(VERSION 3.18)
project(TODO_your_project_name VERSION 1.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion")
include(CTest)

set(CPP_SRCS  # TODO: add all source files
  main.cpp
  file1.cpp
  ... .cpp
)

find_package(wxWidgets REQUIRED COMPONENTS net core base)
if(wxWidgets_USE_FILE) # not defined in CONFIG mode
    include(${wxWidgets_USE_FILE})
endif()

add_executable(${PROJECT_NAME} ${CPP_SRCS})
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

Download the above template CMakeLists.txt.

CMake allows creating out of source builds. This is usually done by your IDE. On the command-line, within your project directory, you'd have to type

mkdir -p build && cd build
cmake ..
make -j8  # -j defines the number of cores to use for compilation

You binary can then be found in the above created build directory.

wxWidgets Example Programs

There are a series of high quality examples available directly in the wxWidgets documentation. These examples can be installed via

sudo apt install wx3.2-examples  # recommended way

or by cloning wxWidgets and entering the samples directory

git clone git@github.com:wxWidgets/wxWidgets.git
cd wxWidgets/samples

In addition, you can find tested, working example code in the C++ wxWidgets Examples repository.