Cross compile with CGO and GoReleaser

How to overcome errors using CGO and GoReleaser by using Docker

Rob De Feo
May 10, 2019 · Unlisted
Image for post
Image for post

I contribute to an open source project, Mailchain, developed in Golang. I want to easily create releases using CI/CD. Golang is a compiled language that allows for a simple way to compile your code, to execute on different operating systems. I found the fantastic tool, which builds, releases, and publishes the binaries.

This is the command to build on a Mac.

  • rm-dist A clean distribution directory is required this ensures it is removed.
  • snapshot By default a release is set to publish. This flag turns off this action.

When I ran this on my Mac, it fails when building the binary for linux.

• releasing using goreleaser 0.106.0...
• loading config file file=.goreleaser.yml
• building binary=dist/darwin_amd64/mailchain
• building binary=dist/linux_amd64/mailchain
⨯ release failed after 9.40s error=failed to build for linux_amd64: # os/user
/usr/local/Cellar/go/1.12.4/libexec/src/os/user/getgrouplist_unix.go:16:35: warning: passing 'gid_t *' (aka 'unsigned int *') to parameter of type 'int *' converts between pointers to integer types with different sign [-Wpointer-sign]
ld: warning: option -s is obsolete and being ignored
ld: warning: ignoring file
"__cgoexp_07a0021afc18_secp256k1GoPanicError", referenced from:
_secp256k1GoPanicError in 000023.o
"__cgoexp_07a0021afc18_secp256k1GoPanicIllegal", referenced from:
_secp256k1GoPanicIllegal in 000023.o
"_crosscall2", referenced from:
_secp256k1GoPanicIllegal in 000023.o
_secp256k1GoPanicError in 000023.o
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Whats going on???

The problem is that one library has a dependency on C and requires CGO. I like having GoReleaser in my tool kit, a lot! I looked deeper into the issue. It’s a case of ensuring all the right libraries are installed for CGO to compile against different operating systems. Docker seems and excellent fit, there’s even a docker container for CGO.

  • rm Starts with a fresh docker container each time.
  • -v ($pwd):/go/src/ Creates a volume on the docker container with the contents on of the current directory.
  • -w /go/src/ the working directory to the code that was created in the go/srcdirectory.

Note: remember to replace :organd :repo with your repository details.

releasing using goreleaser 0.106.0...
loading config file file=.goreleaser.
building binary=dist/darwin_amd64/mailchain
⨯ release failed after 163.82s error=failed to build for darwin_amd64: go build build constraints exclude all Go files in /go/pkg/mod/
/go/pkg/mod/ undefined: ipcListen
/go/pkg/mod/ undefined: newIPCConnection

We are making progress…

But I get a different error this time that does not look related to CGO. It is though, is the location of the C dependency. Looks like I need is a docker container that has CGO libraries and is able to cross compile into Linux, OSX, and Windows. Enter goreleaser-xcgo, a docker container with all of the required binaries and the latest version of GoReleaser. Usage is similar to before.

Success!!! :)

This time it creates all the binaries are built.

• releasing using goreleaser 0.106.0...
• building binary=dist/darwin_amd64/mailchain
• building binary=dist/linux_amd64/mailchain
• building binary=dist/windows_amd64/mailchain.exe
• release succeeded after 330.99s

To see it working the configuration file .goreleaser.yml is here, to see how its integrated with travis here is the .travis.yml configuration.

If you want to find out more about what we are building at Mailchain find us at


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store