FLTK
FLTK is a small, cross-platform C++ GUI toolkit. This builds it from source on Windows with the MSYS2 UCRT64 toolchain (see C++ for the toolchain setup), installs it to a prefix, then compiles a hello-world app against it with CMake.
Run everything from the MSYS2 UCRT64 shell.
Install the toolchain
pacman -Syu # update first; reopen the shell if it asks
pacman -S --needed \
git \
unzip \
mingw-w64-ucrt-x86_64-toolchain \
mingw-w64-ucrt-x86_64-cmake \
mingw-w64-ucrt-x86_64-ninja \
mingw-w64-ucrt-x86_64-zlib \
mingw-w64-ucrt-x86_64-libpng \
mingw-w64-ucrt-x86_64-libjpeg-turbo
zlib, libpng and libjpeg-turbo let FLTK read those image formats; drop
them if you do not need image support.
Build FLTK from source
Clone the repository and check out a release tag:
FLTK_VER=1.4.4
mkdir -p ~/fltk/src && cd ~/fltk/src
git clone https://github.com/fltk/fltk.git fltk
cd fltk
git checkout release-$FLTK_VER
Configure a static build, install it to a dedicated prefix, then build and install:
cmake -S . -B build-ucrt64-static -G Ninja \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_INSTALL_PREFIX=/opt/fltk-ucrt64-static \
-D FLTK_BUILD_TEST=OFF
cmake --build build-ucrt64-static
cmake --install build-ucrt64-static
Keep -D FLTK_BUILD_TEST=OFF. With the test programs enabled, the build can
export broken library references (the demo executables get built, but the
installed CMake targets your own project links against come out wrong). Turning
the tests off leaves a clean, linkable install.
Verify the install exported its CMake config and libraries:
find /opt/fltk-ucrt64-static -name "FLTKConfig.cmake" 2>/dev/null
ls -1 /opt/fltk-ucrt64-static/lib
For shared libraries instead, configure into a separate build and prefix
with FLTK_BUILD_SHARED_LIBS=ON, then build and install it the same way:
cmake -S . -B build-ucrt64-shared -G Ninja \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_INSTALL_PREFIX=/opt/fltk-ucrt64-shared \
-D FLTK_BUILD_TEST=OFF \
-D FLTK_BUILD_SHARED_LIBS=ON
cmake --build build-ucrt64-shared
cmake --install build-ucrt64-shared
With a shared build the resulting .exe needs libfltk*.dll on the PATH at
run time; the static build has no such dependency.
Build an app against FLTK
Create a small project:
mkdir -p ~/fltk/src/fltk-hello
cd ~/fltk/src/fltk-hello
main.cpp — a window with a button that sets a label when clicked:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
static Fl_Box* g_text = nullptr;
static void on_click(Fl_Widget*, void*) {
if (g_text) {
g_text->label("Hello");
g_text->redraw();
}
}
int main() {
Fl_Window win(360, 180, "FLTK Hello");
Fl_Box text(20, 25, 320, 50, "");
text.labelsize(28);
g_text = &text;
Fl_Button btn(130, 100, 100, 40, "Press");
btn.callback(on_click);
win.end();
win.show();
return Fl::run();
}
CMakeLists.txt — find the installed FLTK by its CMake config and link the
fltk::fltk target:
cmake_minimum_required(VERSION 3.20)
project(fltk_hello_button LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(FLTK CONFIG REQUIRED)
add_executable(fltk_hello main.cpp)
target_link_libraries(fltk_hello PRIVATE fltk::fltk)
Configure, build and run. CMAKE_PREFIX_PATH points CMake at the prefix you
installed FLTK into:
cmake -S . -B build -G Ninja \
-D CMAKE_BUILD_TYPE=Debug \
-D CMAKE_PREFIX_PATH=/opt/fltk-ucrt64-static
cmake --build build
./build/fltk_hello.exe
To link the shared build instead, point CMAKE_PREFIX_PATH at
/opt/fltk-ucrt64-shared.
Clean up
rm -rf ~/fltk
rm -rf /opt/fltk-ucrt64-static /opt/fltk-ucrt64-shared