Methods of payment Abuse

Cross-compiling Gotk3 applications for Windows to Linux

22.05.2021, 22:33

What is the Gotk3 library? It is primarily a set of bindings of GTK3, a graphical application creation tool popular in Linux for the GoLang programming language. You can build anything in Linux without bugs. However, Golang programs are cross-platform and can be built for Windows as well, so the user may have some difficulties.

The problem is that the Ubuntu environment produces a sea of errors during compilation. One of the frequent errors is fatal error: libintl.h: No such file or directory. It indicates a GoLang problem with finding the right C header files. But this problem does not exist in ArchLinux.

Cross-compiling Gotk3 applications

For the purpose of cross-compiling, users are recommended to use ArchLinux with DockerHub. Sometimes errors occur and after downloading the image, you will see how to fix them. The first step is to download the image and start the container by mounting a folder with your project in it using volume mount. For example, to the /project folder:

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

By using the --rm option, you can delete the container after exiting it. This is done to free up space on your hard disk. There is a problem with glibc in the latest ArchLinux image, because of which pacman does not work. To fix it, run this 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"

There are also sometimes problems with keys. This is not a working system, so you should just disable package verification so you don't have to search for a solution every time. How to do this. To do this, in the [options] section of the /etc/pacman.conf file, change the SigLevel parameter to Never:

$ vim /etc/pacman.conf
[options]
SigLevel = Never

Next, update the package database:

$ pacman -Sy

Install the required packages:

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

We also need one package from AUR. 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 install the package:

$ pacman -S mingw-w64-gtk3

Separately, it should be noted that the latest version of Golang already includes Go modules. If for some reason they are not used in your project, you will have to start using them. It is better to configure the modules and project dependencies on your working system and check that everything is built correctly for Linux. The gotk3 version should be taken from the master branch, because older versions are not built with this version of the language:

$ got get github.com/gotk3/gotk3 master

Next, 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 may take quite a long time, but after it is finished you will see the finished file in the project folder.

When the installation is complete, the user will see the finished file in the project folder:

Cross-compiling Gotk3 applications for Windows to Linux

To avoid repeating the same steps time after time, use a special Dockerfile:

$ 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

You may have to tinker with it and make changes to get it to work.

You need to create a separate directory, place this file in it and execute it:

$ docker build

After executing the command, the ID of the new container will appear:

Cross-compiling Gotk3 applications for Windows to Linux

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

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

This completes the instruction.