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

how to build for linux on macOS #465

Open
newtorn opened this issue Jun 3, 2020 · 11 comments
Open

how to build for linux on macOS #465

newtorn opened this issue Jun 3, 2020 · 11 comments

Comments

@newtorn
Copy link

newtorn commented Jun 3, 2020

No description provided.

@nasermirzaei89
Copy link

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

@veeableful
Copy link
Contributor

Hi @newtorn, did you try this command?

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

@nasermirzaei89
Copy link

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?

@veeableful
Copy link
Contributor

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

@pavankumar-go
Copy link

pavankumar-go commented Aug 17, 2020

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)

@veeableful
Copy link
Contributor

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
Copy link
Contributor

veeableful commented Aug 19, 2020

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!

@pavankumar-go
Copy link

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

@nasermirzaei89
Copy link

nasermirzaei89 commented Aug 20, 2020

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

@pavankumar-go
Copy link

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

@veeableful
Copy link
Contributor

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.

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

4 participants