go-sdl2
go-sdl2 copied to clipboard
how to build for linux on macOS
Try to build in Docker or you can build in CI and grab binary from artifacts (in GitLab)
Hi @newtorn, did you try this command?
CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"
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?
Hi @nasermirzaei89, yes I believe so. It points to the clang
compiler.
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)
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.
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!
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
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
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
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.