Jonny
Jonny

Reputation: 353

CGO: Cross compile from mac/windows to linux amd64 with cgo_enabled = 1

I target a project with the use of gosseract to cross compile, which need to have CGO_ENABLE=1. After a lot of tries I ended up compiling Mac and Windows native. Mac on my laptop and Windows in a foreign project VM using msys2 and mingw.

But I do not want a third machine for Linux. I tested some Docker solutions, but they never worked for Windows or Mac.

So I would like to use the Mac or the Windows VM (with msys2 & pacman) to compile against Linux amd64.

My commands for Mac:

env GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build -o ../build/darwin/fileInspector

for Windows:

env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" go build -o ../build/windows/fileInspector.exe

for Linux from Mac with errors:

env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CGO_CXXFLAGS="-I/usr/local/Cellar/leptonica/1.82.0/include -I/usr/local/Cellar/tesseract/5.1.0/include" go build -o ../buidl/linux/fileInspector
linux_syscall.c:67:13: error: implicit declaration of function 'setresgid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
linux_syscall.c:67:13: note: did you mean 'setregid'?
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/unistd.h:593:6: note: 'setregid' declared here
linux_syscall.c:73:13: error: implicit declaration of function 'setresuid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
linux_syscall.c:73:13: note: did you mean 'setreuid'?
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/unistd.h:595:6: note: 'setreuid' declared here
make: *** [gobuildLinux] Error 2

for Linux from Windows with errors:

env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" go build -o ../build/windows/fileInspector.exe
gcc_linux_amd64.c: In function '_cgo_sys_thread_start':
gcc_linux_amd64.c:61:9: error: unknown type name 'sigset_t'; did you mean '_sigset_t'?
   61 |         sigset_t ign, oset;
      |         ^~~~~~~~
      |         _sigset_t
gcc_linux_amd64.c:66:9: error: implicit declaration of function 'sigfillset' [-Werror=implicit-function-declaration]
   66 |         sigfillset(&ign);
      |         ^~~~~~~~~~
gcc_linux_amd64.c:61:23: error: unused variable 'oset' [-Werror=unused-variable]
   61 |         sigset_t ign, oset;
      |                       ^~~~
cc1.exe: all warnings being treated as errors
make: *** [Makefile:56: gobuildLinux] Error 2

I probably need a different compiler, but I have no clue.

Upvotes: 4

Views: 3747

Answers (1)

Cytown
Cytown

Reputation: 1547

You should try docker/podman for cross platform build, or you should meet terrible issues...

Upvotes: 1

Related Questions