Search code examples
gocgo

Build Go with C++ (cgo) via docker linux x64 for linux i386


I use the docker golang:1.22 for building my Go project for Windows x32/x64 and Linux x32/x64.

I have installed:

  • apt -y install build-essential
  • apt -y install gcc-multilib
  • apt -y install g++-multilib

It has next versions:

  • go version go1.22.3 linux/amd64
  • gcc (Debian 12.2.0-14) 12.2.0
  • cgo version go1.22.3

Now I can build windows x32/x64 and linux x64, but I have a problem with linux i386.

Windows x32 (it works)

GOOS=windows GOARCH=386 CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-mingw32-gcc go build -o core_joint_x32.exe cmd/core/main.go

Windows x64 (it works)

GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc go build -o core_joint_x64.exe cmd/core/main.go

Linux x64 (it works)

GOARCH=amd64 go build -o core_joint_x64 cmd/core/main.go

But I don't know, what compiler I should using, what flags for Linux i386 ...

Thank you for your answers!


Solution

  • I've found, this works:

    GOARCH=386 CGO_ENABLED=1 CXX='g++ -m32' CC='gcc -m32' go build -o core_joint_i386 cmd/core/main.go