Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross compile from Mac to Linux #1040

Open
batara666 opened this issue May 12, 2022 · 6 comments
Open

cross compile from Mac to Linux #1040

batara666 opened this issue May 12, 2022 · 6 comments

Comments

@batara666
Copy link

> go version
go version go1.18.1 darwin/arm64
> GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -trimpath
# runtime/cgo
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
@mattn
Copy link
Owner

mattn commented May 12, 2022

Your C compiler seems not support cross compiling for Linux.

@malaschitz
Copy link

I ended up using Docker. I don't know much about it, but I managed. Command:

docker buildx build --platform=linux/amd64 --target bin --output bin/ .

And here's the important part for from dockerfile:


FROM --platform=$BUILDPLATFORM golang:latest AS build

WORKDIR /src

RUN apt-get update 
RUN apt-get install -y gcc-multilib

ENV CGO_ENABLED=1
ARG TARGETOS TARGETARCH

RUN --mount=target=. \
    --mount=type=cache,target=/root/.cache/go-build \
    --mount=type=cache,target=/go/pkg \
    GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/fillip_linux ./cmd

FROM scratch as bin

COPY --from=build /out/fillip_linux ./fillip_linux

@motiejus
Copy link

You can use zig as a cross-compiler (many references on the internet how to do it). I am on linux/amd64, so will cross-compile _examples/simple to linux/arm64:

go-sqlite3/_example/simple$ CC="zig cc -target aarch64-linux-musl" GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -trimpath  .
go-sqlite3/_example/simple$ file simple
simple: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), static-pie linked, Go BuildID=v0J3H_Y7IQRiQ26cF8Rb/DJMzPVHNAPPpixbjkZNj/Ej2fxeIvLwu1tw80enDk/nnoFyKfir37KeWzAv6b0, with debug_info, not stripped
go-sqlite3/_example/simple$ qemu-aarch64-static ./simple | head -10
0 こんにちわ世界000
1 こんにちわ世界001
2 こんにちわ世界002
3 こんにちわ世界003
4 こんにちわ世界004
5 こんにちわ世界005
6 こんにちわ世界006
7 こんにちわ世界007
8 こんにちわ世界008
9 こんにちわ世界009

You can also use, for example, -target aarch64-linux-gnu.2.32 (or so) to link against glibc 2.32 instead of producting a musl-based static binary.

@LittleKey
Copy link

You can use zig as a cross-compiler (many references on the internet how to do it). I am on linux/amd64, so will cross-compile _examples/simple to linux/arm64:

go-sqlite3/_example/simple$ CC="zig cc -target aarch64-linux-musl" GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -trimpath  .
go-sqlite3/_example/simple$ file simple
simple: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), static-pie linked, Go BuildID=v0J3H_Y7IQRiQ26cF8Rb/DJMzPVHNAPPpixbjkZNj/Ej2fxeIvLwu1tw80enDk/nnoFyKfir37KeWzAv6b0, with debug_info, not stripped
go-sqlite3/_example/simple$ qemu-aarch64-static ./simple | head -10
0 こんにちわ世界000
1 こんにちわ世界001
2 こんにちわ世界002
3 こんにちわ世界003
4 こんにちわ世界004
5 こんにちわ世界005
6 こんにちわ世界006
7 こんにちわ世界007
8 こんにちわ世界008
9 こんにちわ世界009

You can also use, for example, -target aarch64-linux-gnu.2.32 (or so) to link against glibc 2.32 instead of producting a musl-based static binary.

Work for me!

Macos compile to arm64-linux

brew install zig
CC="zig cc -taget aarch64-linux-musl" CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build xxx.go -o executable

@steffenfritz
Copy link

brew install zig
CC="zig cc -taget aarch64-linux-musl" CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build xxx.go -o executable

Following your comment @LittleKey, I tried the same but ended up with 7 errors (warnings omitted by flag). Does anyone know how to handle this? This really looks like a problem with the bindings, in combination with the more strict zig.

me@here ~/g/s/g/s/F/c/ft (main) [1]> CC="zig cc -Wno-everything -target aarch64-linux-musl" CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build
# github.com/mattn/go-sqlite3
sqlite3-binding.c:37644:42: error: use of undeclared identifier 'pread64'
  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
                                         ^
sqlite3-binding.c:37662:42: error: use of undeclared identifier 'pwrite64'
  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
                                         ^
sqlite3-binding.c:37801:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall[]'
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
                     ^~~~~~~~~~
sqlite3-binding.c:37810:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall[]'
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
                     ^~~~~~~~~~
sqlite3-binding.c:37837:20: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall[]'
  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
                   ^~~~~~~~~~
sqlite3-binding.c:37854:16: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall[]'
    for(i=0; i<ArraySize(aSyscall)-1; i++){
               ^~~~~~~~~~~~~~~~~~~
sqlite3-binding.c:14847:38: note: expanded from macro 'ArraySize'
#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
                                     ^~~
sqlite3-binding.c:37858:14: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall[]'
  for(i++; i<ArraySize(aSyscall); i++){
             ^~~~~~~~~~~~~~~~~~~
sqlite3-binding.c:14847:38: note: expanded from macro 'ArraySize'
#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
                                     ^~~
7 errors generated.

@JonathanHope
Copy link

There is a solution in this issue: #1164.

CGO_CFLAGS="-D_LARGEFILE64_SOURCE"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants