Giter Club home page Giter Club logo

xcompile-rpi-cgo-musl's Introduction

Instructions to cross-compile from macOS High Sierra to Raspberry Pi 3 Model B armv7l against musl libc

  1. Run brew install FiloSottile/musl-cross/musl-cross --without-x86_64 --with-arm-hf

This will take some time (my installation took ~45 minutes), but afterwards, a number of compilers for musl will now be in /usr/local/bin. Specifically, the one that we're concerned with is arm-linux-musleabihf-gcc which will replace the regular gcc when we call go build or vgo build to generate a binary.

  1. Create go code inclusive of cgo (i.e. import "C")

  2. To compile a dynamic binary against musl libc for Rpi use the following build command:

env CC=arm-linux-musleabihf-gcc GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 vgo build -o some_name /some/path/to/main.go

  1. To compile a static binary against musl libc for Rpi use the following build command:

env CC=arm-linux-musleabihf-gcc GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 vgo build -o some_name --ldflags '-linkmode external -extldflags "-static"' /some/path/to/main.go

(Yes, vgo build works, too!)

The difference in size between these two will be minimal, unlike when using glibc where there is a greater delta between dynamic binaries linking to glibc directly and static.

Also, if you fail to include CGO_ENABLED=1 in the build command, compilation will fail. The compiler will ignore any files containing the import "C" directive and an error on compilation will come up stating that go symbols are undefined. This bit was confusing and threw me off. I couldn't figure out why the compiler was complaining a go func was undefined. I ran the build using the -x switch and realized that the particular file importing C was ignored. To ensure that these go files with cgo in them are included, you must explicitly tell the build command to enable cgo.

  1. Copy or move binary output to Rpi via scp or samba.

  2. Execute binary on Rpi. Ta da! It works! And your cgo code will call C using musl libc.

xcompile-rpi-cgo-musl's People

Contributors

bsandusky avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.