This post is originally from my blog. If you have trouble reading this on Medium, you can follow the link.
Okay, if you are reading this, you may (probably) have been searching for something like “rust gtk getting started” on Google. Most of them points to another awesome guides such as here
I wrote this guide for one purpose: To share what I have just learned. Rust is an interesting language and I decided to get along with it (after years of working with JavaScript, Java and Dart).
But why GTK, you ask? At the time of writing:
- Azul is still in alpha. And the output binary is too large (5MB for a simple HelloWorld app).
- Iced is in 0.1 (it’s good still, I may create a new writeup for this later).
- gtk-rs is complete and stable already.
Trust me, I had a hellish time with stuff like Cmake, MSYS, MinGW,… on Windows. No, I don’t hate them, but there was a time when the term “A software distribution and building platform on Windows” doesn’t make sense to me. I just… don’t get it at all.
But I know I have to, eventually. And there it is! I bumped into this article via Google and I realized something I should a long time ago:
- Msys / Msys2 / MSVC,… they are the name of “toolchains”, a collection of software to get something (mostly app development) done. Microsoft provides Visual Studio and Visual Studio Build Tools (which we might know as MSVC), it’s great. And yes, it’s locked into Windows ecosystem. But if you’re interested in cross-platform development (like me), then you will want Msys2 and its fabulous GNU toolchain. It consists of 3
subsystems
, from theIntroduction
section:
The mingw subsystems provide native Windows programs and are the main focus of the project. These programs are built to co-operate well with other Windows programs, independently of the other subsystems. This part builds on the MinGW-w64 project.
The
msys2
subsystem provides an emulated mostly-POSIX-compliant environment for building software, package management, and shell scripting. These programs live in a virtual single-root filesystem (the root is the MSYS2 installation directory). Some effort is made to have the programs work well with native Windows programs, but it’s not seamless. This part builds on the Cygwin project.Each of the subsystems provides its own native (i.e. target=host) compiler toolchain, in
msys2-devel
,mingw-w64-i686-toolchain
, andmingw-w64-x86_64-toolchain
. There are also cross compiler toolchains withhost={i686,x86_64}-pc-msys
andtarget={i686,x86_64}-w64-mingw32
inmingw-w64-cross-toolchain
, but these are of limited use because there are no library packages for them.
1. Installing Rust
The best way is to follow official instruction. If you are on Windows (probably, because that’s why you are reading this post), you’d better go with downloading rust-init.exe
(64-bit version).
After installing, check your rust installation with:
rustc --version
To update Rust itself, use:
rustup update
2. Add the GNU toolchain
Yes, this is the most important part. Simply use this command:
rustup target add x86_64-pc-windows-gnu
Because gtk-rs doesn’t support MSVC toolchain yet, you will bump into several problems if you forgot to add that target. Then use this command to show current targets:
rustup show
You will see something like this:
stable-x86_64-pc-windows-gnu
stable-x86_64-pc-windows-msvc (default)
To switch to GNU toolchain, use this:
rustup default stable-x86_64-pc-windows-gnu
3. Install MSYS2
First, grab the installer here
Then set these environment variables. You can see it by right-clicking on “This PC” > Properties > Advanced System Settings and set following paths:
GTK_LIB_DIR=C:\msys64\mingw64\lib
C:\msys64\mingw64\bin // Append this to PATH
Next, use following commands in MSYS Shell
(NOT Command Prompt
or Windows PowerShell
):
pacman -S mingw-w64-x86_64-gtk3
pacman -S mingw-w64-x86_64-toolchain
By this way, MSYS will download necessary toolchain to compile GTK apps, then our project will be able to find it by looking up the environment variables.
If there are missing dependencies, you can easily install them by using MSYS Shell with pacman
. This command can cover most of them (including cmake
, automake
and sed
)
pacman -S base-devel gcc vim cmake
Now it’s on, you should be able to get GTK app up and running. Please kepe in mind that gtk-rs is not supporting MSVC at the moment. Track the issue here.