Cross Compiling go-sqlite3 for ARM7 from Debian 9

So, here I am documenting this “how-to” and hoping that it helps someone save some hair, hours poring through 100s of webpages and dozens of trial-and-errors. So here was the task.

I had to develop a wallboard (and reporting solution) for an asterisk/ARM based equipment (by now you can guess it’s a yeastar “S20” device). After evaluating my options (nodejs arm or go), I went with go. I’d used it in the past.

Interaction will be via a web-browser, and since I had just started getting my SPA groove on, I decided to go the route of SPA. Have a fat javascript front-end, and a not-so-fat go application (with all assets bundled into a single executable) and sqlite as RDBMS. I won’t go into the details of queue_logs, asterisk AMI and stuff so let’s get into the meat of this post.

With go, you can target your builds for several architecture/OS combinations and it’s easy to accomplish. However, when it’s time to build go applications that plug into C+

+ (for a different architecture), things get a little complicated. So I’ll assumed that;

  1. Your development machine is running debian 8 (or 9)
  2. Your are targeting the ARM7 arch
  3. You’re not familiar with cross-compiling tools
  4. You’re probably using the go-sqlite3 in your project and tried this, only to copy your executable over to your ARM7 device and get an ominousĀ “Not Found” after running ./yourExec for a file that is obviously there.

The solution is to statically link a few dependencies which your executable needs, as they are unavailable on the target machine.

Here’s a step by step;

  1. Don’t bother trying to get this to work on debian 8. I had to setup a debian 9 linode.
  2. If you plan to use linode, modify the sources.list to use the original debian stretch debs, not the linode mirrors
  3. Run:
    sudo dpkg --add-architecture armhf
    sudo apt-get update
    sudo apt-get install build-essential crossbuild-essential-armhf
  4. At this stage, you should be able to run arm-linux-gnueabihf-gcc and get a no input files fatal error
  5. Then install go, git, my project etc. into the newly setup debian 9 VM.
  6. Drop into your go project directory and run a build. Comparing the command below to the one on the README page of go-sqlite3, you will notice that -static is really what’s differentCGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ go build -ldflags "-v '-extldflags=-v -static'"a
  7. Since this is a build, you should find the executable in your project folder. Copy this over to your ARM device and you should be good to go.

If things don’t go right the first time, have a little more patience and remember google is your friend.

 

Links

  • http://jensd.be/800/linux/cross-compiling-for-arm-with-ubuntu-16-04-lts

Leave a Reply