Cross-Compile C++ for Raspberry Pi Zero W
I have been a fan of CMake and the conan package manager for some time when building C++.
Install the Toolchain
Install the toolchain from https://gnutoolchains.com/raspberry/. Specifically, I chose 'raspberry-gcc8.3.0-r3.exe
‘ and installed to the default location.
Install CMake
Download and install the latest version of CMake from https://cmake.org/download/. Specifically, I used ‘cmake-3.19.3-win64-x64.msi
‘. I think that it automatically added to the PATH.
Add Conan Profiles
First thing, install conan if you haven’t (pip install conan
). Create the following files in C:\<user>\.conan\profiles\
.
raspberrypi-arm6
include(user-raspberrypi-x86_64)
target_host=arm-linux-gnueabihf
cc_compiler=gcc
cxx_compiler=g++
[settings]
os_build=Windows
arch_build=x86_64
os=Linux
arch=armv6
compiler=gcc
compiler.version=8
compiler.libcxx=libstdc++11
build_type=Release
[env]
CONAN_CMAKE_FIND_ROOT_PATH=$standalone_toolchain/$target_host/sysroot
PATH=[$standalone_toolchain/bin]
CHOST=$target_host
AR=$target_host-ar
AS=$target_host-as
RANLIB=$target_host-ranlib
LD=$target_host-ld
STRIP=$target_host-strip
CC=$target_host-$cc_compiler
CXX=$target_host-$cxx_compiler
CXXFLAGS=-I"$standalone_toolchain/$target_host/lib/include"
user-raspberrypi-x86_64
This file is meant for user / computer-specific settings. This is mainly only for if multiple team members are sharing the configuration.
standalone_toolchain=C:/sysgcc/raspberry
Build
For the purpose of this demo, the use of conan has limited value, but comes in super handy for running unit-tests prior to building the target application.
# Install Dependencies
conan install . --install-folder build-raspberry-armv6 -pr=raspberrypi-arm6
# Build
conan build . --build-folder build-raspberry-armv6
Console Output
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS D:\Projects\blog\raspberry-pi-zero-demo>
PS D:\Projects\blog\raspberry-pi-zero-demo> cd .\code\hello-world\
PS D:\Projects\blog\raspberry-pi-zero-demo\code\hello-world> conan install . --install-folder build-raspberry-armv6 -pr=raspberrypi-arm6
Configuration:
[settings]
arch=armv6
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=8
os=Linux
os_build=Windows
[options]
[build_requires]
AR=arm-linux-gnueabihf-ar
AS=arm-linux-gnueabihf-as
CC=arm-linux-gnueabihf-gcc
CHOST=arm-linux-gnueabihf
CONAN_CMAKE_FIND_ROOT_PATH=C:/sysgcc/raspberry/arm-linux-gnueabihf/sysroot
CXX=arm-linux-gnueabihf-g++
CXXFLAGS=-I"C:/sysgcc/raspberry/arm-linux-gnueabihf/lib/include"
LD=arm-linux-gnueabihf-ld
PATH=[C:/sysgcc/raspberry/bin]
RANLIB=arm-linux-gnueabihf-ranlib
STRIP=arm-linux-gnueabihf-strip
conanfile.py: Installing package
Requirements
Packages
Cross-build from 'Windows:x86_64' to 'Linux:armv6'
Installing (downloading, building) binaries...
conanfile.py: Generator txt created conanbuildinfo.txt
conanfile.py: Generated conaninfo.txt
conanfile.py: Generated graphinfo
PS D:\Projects\blog\raspberry-pi-zero-demo\code\hello-world> conan build . --build-folder build-raspberry-armv6
Using lockfile: 'D:\Projects\blog\raspberry-pi-zero-demo\code\hello-world\build-raspberry-armv6/conan.lock'
Using cached profile from lockfile
conanfile.py: Calling build()
-- The CXX compiler identification is GNU 8.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/SysGCC/raspberry/bin/arm-linux-gnueabihf-g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
BUILD_SHARED_LIBS
CMAKE_EXPORT_NO_PACKAGE_REGISTRY
CMAKE_INSTALL_BINDIR
CMAKE_INSTALL_DATAROOTDIR
CMAKE_INSTALL_INCLUDEDIR
CMAKE_INSTALL_LIBDIR
CMAKE_INSTALL_LIBEXECDIR
CMAKE_INSTALL_OLDINCLUDEDIR
CMAKE_INSTALL_SBINDIR
CONAN_CMAKE_FIND_ROOT_PATH
CONAN_COMPILER
CONAN_COMPILER_VERSION
CONAN_EXPORTED
CONAN_IN_LOCAL_CACHE
CONAN_LIBCXX
PORT
-- Build files have been written to: D:/Projects/blog/raspberry-pi-zero-demo/code/hello-world/build-raspberry-armv6
Scanning dependencies of target helloworld
[ 50%] Building CXX object CMakeFiles/helloworld.dir/main.cpp.o
[100%] Linking CXX executable helloworld
[100%] Built target helloworld
PS D:\Projects\blog\raspberry-pi-zero-demo\code\hello-world>
Code
The latest profiles and demo source is found here: https://github.com/echozulucode/raspberry-pi-zero-demo.