From b9f760a2b3867113ab511a81ecabd56f93447c84 Mon Sep 17 00:00:00 2001 From: Sebastian Detert Date: Sat, 14 Sep 2024 21:19:46 +0200 Subject: [PATCH] Add description to cross compile for specific libc dependency Can be used to compile for raspberry 4 (rasbian bullseye for example) --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index 1804a89..e0eff8d 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ This package follows the official [Golang Release Policy](https://golang.org/doc - [Android](#android) - [ARM](#arm) - [Cross Compile](#cross-compile) + - [Cross Compiling from macOS](#cross-compiling-from-macos) + - [Cross Compile for specific libc version](#cross-compile-for-specific-libc-version) - [Google Cloud Platform](#google-cloud-platform) - [Linux](#linux) - [Alpine](#alpine) @@ -228,6 +230,48 @@ Steps: Please refer to the project's [README](https://github.com/FiloSottile/homebrew-musl-cross#readme) for further information. +## Cross Compile for specific libc version + +If you need to cross compile and the remote machine uses an older libc version, then you can use a simple Dockerfile (see below) to create a matching image and compile using docker + +```dockerfile +# Dockerfile to compile your go application +# with the libc version 2.31-13 as used by debian:bullseye or on Raspbian (Raspberry 4 bullseye) + +# check your libs version using `ldd --version` and download the matching golang image if needed +FROM golang:1.23-bullseye AS build + +RUN echo $(ldd --version) + +# Install cross-compilation tools +RUN apt-get update && apt-get install -y \ + gcc-arm-linux-gnueabihf \ + g++-arm-linux-gnueabihf \ + libc6-dev-arm64-cross \ + libsqlite3-dev \ + sqlite3 + +# Set environment variables for cross-compilation +ENV GOARM=7 +ENV GOARCH=arm +ENV GOOS=linux +ENV CGO_ENABLED=1 +ENV CC=arm-linux-gnueabihf-gcc +ENV CXX=arm-linux-gnueabihf-g++ + +# Set the working directory +WORKDIR /app + +# Default command: compile the file specified by the argument +ENTRYPOINT ["sh", "-c", "go build -buildvcs=false -o myapp"] + +# build image using +# docker build -t myapp-arm64 . + +# compile your go code with +# docker run --rm -v $(pwd):/app myapp-arm64 +``` + # Google Cloud Platform Building on GCP is not possible because Google Cloud Platform does not allow `gcc` to be executed.