go-sdl2 icon indicating copy to clipboard operation
go-sdl2 copied to clipboard

how to build for linux on macOS

Open newtorn opened this issue 4 years ago • 11 comments

newtorn avatar Jun 03 '20 17:06 newtorn

Try to build in Docker or you can build in CI and grab binary from artifacts (in GitLab)

nasermirzaei89 avatar Jun 04 '20 19:06 nasermirzaei89

Hi @newtorn, did you try this command?

CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"

veeableful avatar Jun 05 '20 02:06 veeableful

Hi @newtorn, did you try this command?

CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"

Is gcc in macOS useful for building Linux binaries?

nasermirzaei89 avatar Jun 05 '20 07:06 nasermirzaei89

Hi @nasermirzaei89, yes I believe so. It points to the clang compiler.

veeableful avatar Jun 05 '20 07:06 veeableful

Need Help!! building for linux on MacOS

CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"
#github.com/veandco/go-sdl2/sdl
ld: unknown option: --no-undefined
clang: error: linker command failed with exit code 1 (use -v to see invocation)

pavankumar-go avatar Aug 17 '20 10:08 pavankumar-go

Hi @pavankumar-go, I believe I could cross-compile to Linux successfully before on macOS but now I also get the same error (though I'm now on the newer macOS Catalina instead of Mojave). It could be caused by the new OS or Xcode updates. What do you think of using Docker to build it? I will create the Docker image later and it can be used as an alternative way to cross-compile to other OS. Linux is our most tested platform for cross-compiling so I thought it might be a good choice.

veeableful avatar Aug 19 '20 04:08 veeableful

I have created a Docker image that I've tried on macOS Catalina to compile go-sdl2 application. After installing and launching Docker, you should be able to go to the application directory and run docker run --rm -e GOOS=linux -e GOARCH=amd64 -e CC=gcc -v $PWD:/opt/app veandco/go-sdl2. Note that I have only made it to work for compiling for Linux 64-bit and Windows 32-bit/64-bit and haven't made it work for other OSes. The output executable should be in the current directory. Please try it and let me know if it works!

veeableful avatar Aug 19 '20 10:08 veeableful

Hi @veeableful tried building using your docker image still no luck :(

Status: Downloaded newer image for veandco/go-sdl2:latest
go: downloading github.com/veandco/go-sdl2 v0.4.4
# github.com/veandco/go-sdl2/img
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status

pavankumar-go avatar Aug 20 '20 05:08 pavankumar-go

I built sample with this Dockerfile:

FROM golang:1.14-alpine as prequisite

RUN apk add build-base libx11-dev sdl2-dev

FROM prequisite AS base

WORKDIR /src
COPY go.mod .
COPY go.sum .
RUN go mod download

FROM base as build
COPY main.go .
RUN go build -v -o game1 main.go

FROM scratch as release
COPY --from=build /src/game1 /

but I didn't test it on Linux!!!

Build with: (Docker Engine 19.03)

DOCKER_BUILDKIT=1 docker build --target release --output type=local,dest=. .

After that, you can find game1 binary in the current directory

nasermirzaei89 avatar Aug 20 '20 06:08 nasermirzaei89

Anyway i wrote one too

FROM ubuntu

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
     apt-get install -y libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libsdl2-gfx-dev wget git gcc 
RUN wget -P /tmp https://dl.google.com/go/go1.13.6.linux-amd64.tar.gz  && \
     tar -C /usr/local -xzf /tmp/go1.13.6.linux-amd64.tar.gz && \
     rm /tmp/go1.13.6.linux-amd64.tar.gz

ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"

WORKDIR $GOPATH/src

COPY . .

RUN go get -v github.com/veandco/go-sdl2/sdl \
    && go get -v github.com/veandco/go-sdl2/img \
    && go get -v github.com/veandco/go-sdl2/mix \
    && go get -v github.com/veandco/go-sdl2/ttf \
    && go get -v github.com/veandco/go-sdl2/gfx \
    && CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"

able to build for linux os

pavankumar-go avatar Aug 20 '20 06:08 pavankumar-go

Ahh yes, I'm sorry. I did not test for other packages, only sdl. I've updated the Docker image to work for other packages as well. You might also want to remove unused images listed in docker images with docker rmi <image ID> as Docker seems to take significant amount of space in macOS.

veeableful avatar Aug 20 '20 07:08 veeableful