Cross compiling Gotk3 App for Windows on Linux (SOLVED)

Cross compiling Gotk3 App for Windows on Linux (SOLVED)

Cross compiling Gotk3 App for Windows on Linux (SOLVED)

The Gotk3 library is a set of bindings for the GTK3 graphical application development tool that is popular in Linux for the GoLang programming language.

So, here, in Linux, everything is constructed quickly and without any errors, but Golang programs are cross-platform, and they can also be compiled for Windows, and this is where the actual problems arise.

It takes time to get all the necessary environments up and running on Windows, and Ubuntu has tons of compile-time errors.

For example, fatal error: libintl.h: No such file or directory. It means that GoLang is having trouble finding the correct C header files.

But there is no such problem in ArchLinux, however, don’t worry about that, as here in this article, I will share how to cross-compile Gotk3 Windows apps in an ArchLinux Docker container.

Cross compiling Gotk3 App for Windows on Linux

For cross-compilation, you can use the latest ArchLinux image from DockerHub. From time to time errors appear there and after downloading the image you will need to fix them.

First, download the image and start the container by forwarding the folder with your project into it using the volume mount. For example, to the /project folder:-

$docker run -it –rm -v ~/go/src/KeywordsMixer/:/project archlinux/archlinux bash

The –rm option allows you to remove the container after exiting it so that it does not take up disk space. The fresh ArchLinux image has a Glibc problem that makes Pacman not work. To fix, run the following command:-

$patched_glibc=glibc-linux4-2.33-4-x86_64.pkg.tar.zst && \
curl -LO “https://repo.archlinuxcn.org/x86_64/$patched_glibc” && \
bsdtar -C / -xvf “$patched_glibc”

While you have to note that by the times you may face problems with keys. This is not a working system, so I prefer to just turn off package verification so that I don’t have to look for a solution to this problem every time.

And to do this, in the [options] section of the /etc/pacman.conf file, change the value of the SigLevel parameter to Never:-

$vim /etc/pacman.conf

[options]
SigLevel = Never

Next, you have to update the package database:-

$pacman -Sy

After that now install all the required packages:-

$pacman -S git go vim mingw-w64-gcc cairo pango pkg-config gtk3

Moreover, we also need one package from the AUR. And to load it, add this section to /etc/pacman.conf:-

$vim /etc/pacman.conf

[ownstuff]
SigLevel = Never
Server = http://martchus.no-ip.biz/repo/arch/$repo/os/$arch

Then you have to install the package:-

$pacman -S mingw-w64-gtk3

Note that the latest version of Golang already includes Go modules, so, if your project is not using them yet, you will have to start using them.

It is better to configure modules and project dependencies on your working system and check that everything builds correctly for Linux. The gotk3 version should be taken from the master branch, as the older versions are not built with this version of the language:-

$got get github.com/gotk3/gotk3 master

Next, you have to go to the project folder and run the build command:-

$cd /project

$CGO_CFLAGS_ALLOW=”.*” CGO_LDFLAGS_ALLOW=”.*” \
PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/lib/pkgconfig CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 \
GOOS=windows GOARCH=amd64 go build -v -tags gtk_3_24 -gcflags “-N -l” -ldflags “-s -w -H=windowsgui” -o main-windows-amd64.exe main.go

The build can take quite a long time, but after its completion, you will see the finished file in the project folder:-

Cross compiling Gotk3 App for Windows on Linux (SOLVED)

Cross compiling Gotk3 App for Windows on Linux (SOLVED)

In order not to do all of the above steps, I prepared a Dockerfile every time. It works at the moment, but it’s not known whether it will work in the future or not, as you may need to correct it a little:-

$vi Dockerfile

FROM archlinux/archlinux
RUN patched_glibc=glibc-linux4-2.33-4-x86_64.pkg.tar.zst && \
curl -LO “https://repo.archlinuxcn.org/x86_64/$patched_glibc” && \
bsdtar -C / -xvf “$patched_glibc”
RUN sed ‘s/SigLevel = Never/SigLevel = Never/g’ /etc/pacman.conf
RUN pacman -Syu –noconfirm
RUN pacman -S git go mingw-w64-gcc cairo pango pkg-config gtk3 –noconfirm
RUN pacman -S vim –noconfirm
RUN echo “[ownstuff]” >> /etc/pacman.conf
RUN echo “SigLevel = Never” >> /etc/pacman.conf
RUN echo ”Server = http://martchus.no-ip.biz/repo/arch/\$repo/os/\$arch” >> /etc/pacman.conf
RUN pacman -Sy –noconfirm
RUN pacman -S mingw-w64-gtk3 –noconfirm

To build an image, just create an empty folder, place this Dockerfile in it and execute the following command:-

$docker build.

In the end, the command will print out the ID of the new container:-

Cross compiling Gotk3 App for Windows on Linux (SOLVED)

Cross compiling Gotk3 App for Windows on Linux (SOLVED)

Then you can start the container based on the resulting image with the command:-

$docker run -it –rm -v ~/go/src/KeywordsMixer/:/project 028451c45c15 bash

Then you can immediately go to the project folder and compile it; what’s the good thing here is that you don’t need to install any components, as you will find that they all are already installed in the image.

So, if you found this article helpful, then do share this with your friends, and on your social profiles.

Add Comment