A Dockerfile to allow cross-compiling C/C++ projects in Ubuntu that will run on a Raspberry Pi.
Note that these instructions were written for a Mac.
To use this container you must pass it a local path that contains a Makefile and a project to compile. In the example below, substitute ~/MY-PROJECTS/MY-BUILD with a path to your local project. Your folder must contain a valid make file called Makefile. The make file must contain references to the Raspberry Pi specific tools that will be described below.
docker run -it -v ~/MY-PROJECTS/MY-BUILD:/build mitchallen/pi-cross-compile
If you had a root project folder called ~/raspberry and a child folder containing your make file and source code called hello you would compile it for Raspberry Pi like this:
docker run -it -v ~/raspberry/hello:/build mitchallen/pi-cross-compile
During the build process, output should be displayed in the terminal. On success, the container will exit and an executable that works on the Pi should be left in your local build directory.
You can find an example project here:
- bitbucket.org/mitchallen/pi-hello-cross-compile.git
- github.com/mitchallen/pi-hello-cross-compile.git
To use the example project do the following:
mkdir ~/raspberry
cd ~/raspberry
git clone https://github.com/mitchallen/pi-hello-cross-compile.git --depth=1 hello
docker run -it -v ~/raspberry/hello:/build mitchallen/pi-cross-compile
On success an executable that only runs on the Raspberry Pi should be found here: ~/raspberry/hello/bin/hello. It's up to you to figure out how to get it on your Pi to run it.
This image contains a call to download Raspberry Cross Compile tools that can be found here: https://github.com/raspberrypi/tools.git. It places them in a folder called pitools.
The actual line is this:
git clone --progress --verbose https://github.com/raspberrypi/tools.git --depth=1 pitools
To cross compile using gcc for Raspberry Pi you would need a line like this in your Makefile:
CC=/pitools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc
For other tools in that folder, browse here.
Once your build is complete, the executable will not work on Ubuntu or a Mac. It will be up to you to copy it over to your Pi and test it.
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
- Added cd command to example usage
- Added links to example and added usage details
- Fixed type-o in README
- Initial release