Building

With all of the appropriate compilers and libraries installed, we can continue to the build phase. For each of the target operating systems, you will need to run through these steps with the correct environment variables set. It is recommended to build for one platform and then complete the packaging step listed in the following table before changing to the next configuration. This is because the release binary for one platform may overwrite another (for example, macOS and Linux binaries have the same name when compiled). Note that when building a release for the current platform, you can omit the CGO_ENABLED and CC environment variables:

Target Platform GOOS= CGO_ENABLED= CC= Notes

macOS

darwin

1

o32-clang

Make sure you've prepared the SDK and copied it to the osxcross tarballs/ directory.

Windows

windows

1

x86_64-w64-mingw32-gcc

A different CC or compiler may be required if you change GOARCH.

Linux

linux

1

gcc-linux

On macOS you may need to use x86_64-linux-musl-gcc for CC.

 

With these environment variables set appropriately, we can execute the build command. You may also want to set GOARCH to specify a different CPU architecture, but doing so is beyond the scope of this chapter. If you are using the Packr asset bundler, then you will need to use the packr build command, otherwise the go build command can be executed. This will typically take longer than a normal run or build, as all of the application dependencies may have to be built for the new target platform before your application can be compiled.

To see this in action, we can open a Linux Terminal and set up the environment for each build. When compiling for the current platform, we don't need to specify the CGO_ENABLED or CC variables as these have correct defaults. After each build, we have a single application binary for the desired platform that we will put to one side and use in the next step (for instance, packaging):

Compiling with CGo for Linux, macOS, and Windows

Once the binary files have been compiled, we could distribute the application – this would be the normal process for command line utilities or web applications. However, with GUI-based applications, a user will expect visual elements, such as icons and integration with application launchers for their platform. That information is added in the packaging phase and varies for each platform and toolkit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.175