Raspberry Pi: C++ cross-compiling

Environment
  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B
Install build tools

Check/install build-essential:

# Check if build-essential is installed
$ dpkg -s build-essntial
# Install build-essential if needed
$ sudo apt-get install build-essential
Install ARM toolchain
# ARM build tools
$ sudo apt-get install g++-arm-linux-gnueabihf
# Check installation
$ arm-linux-gnueabihf-g++ -v
Upgrade libstdc++ to 6.0.21 on Raspberry Pi

See Raspberry Pi: how to solve the “version `GLIBCXX_6.0.21′ not found” problem.

Create helloworld.cpp
#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}
Compile
$ arm-linux-gnueabihf-g++ -O3 -g3 -Wall -fPIC -c -o helloworld.o helloworld.cpp
$ arm-linux-gnueabihf-g++ -o helloworld helloworld.o
Copy helloworld to Raspberry Pi
$ scp ./helloworld pi@192.168.0.xxx:/home/pi/path
Login into Raspberry Pi and run helloworld
$ ssh pi@192.168.0.xxx
# On Raspberry Pi
$ cd path
$ ./helloworld
Hello World!
g++-arm-linux-gnueabihf pakage detais
$ dpkg -s g++-arm-linux-gnueabihf
Package: g++-arm-linux-gnueabihf
Status: install ok installed
Priority: optional
Section: devel
Installed-Size: 11
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Source: gcc-defaults (1.150ubuntu1)
Version: 4:5.3.1-1ubuntu1
Depends: cpp-arm-linux-gnueabihf (>= 4:5.3.1-1ubuntu1), gcc-arm-linux-gnueabihf (>= 4:5.3.1-1ubuntu1), g++-5-arm-linux-gnueabihf (>= 5.3.1-3~)
Description: GNU C++ compiler for the armhf architecture
 This is the GNU C++ compiler, a fairly portable optimizing compiler for C++.
 .
 This is a dependency package providing the default GNU C++ cross-compiler
 for the armhf architecture.
Original-Maintainer: Debian GCC Maintainers <debian-gcc@lists.debian.org>

3 Replies to “Raspberry Pi: C++ cross-compiling”

  1. Hi Yuriy. I’m glad I found your blog. You’ve published very useful information on it.
    As the first thing, I was going to install dart sdk on my Raspberry Pi 3 B and the latest Raspbian OS.
    Unfortunately, I have a problem to install ARM 32-bit toolchain. When I type “sudo apt-get install g++-arm-linux-gnueabihf” then I’m getting an error:
    “E: Unable to locate package g++-arm-linux-gnueabihf
    E: Couldn’t find any package by regex ‘g++-arm-linux-gnueabihf'”
    Do you have an idea when could be the problem?

    1. Hi Yuriy,
      Thanks for your feedback. I’m new to Raspberry Pi and have not used Linux for ages. Yes, I tried to compile it on RPI so ignore that issue.

      I’ve checked more your posts and I have tried to follow the instructions on that post:
      http://sw-eng-notes.geomodule.com/2017/04/16/dart-build-dart-sdk-for-x86-and-arm
      I stuck at the point when I tried to get dart-sdk source.
      When I run “gclient sync” all works fine until the below error:

      ________ running ‘/usr/bin/python sdk/build/linux/sysroot_scripts/install-sysroot.py –running-as-hook –arch i386’ in ‘/home/marcinsydor/dart-sdk’
      Installing Debian Wheezy i386 root image: /home/marcinsydor/dart-sdk/sdk/build/linux/debian_wheezy_i386-sysroot
      Downloading http://storage.googleapis.com/chrome-linux-sysroot/toolchain/a2d45701cb21244b9514e420950ba6ba687fb655/debian_wheezy_i386_sysroot.tgz
      Traceback (most recent call last):
      File “sdk/build/linux/sysroot_scripts/install-sysroot.py”, line 181, in
      sys.exit(main())
      File “sdk/build/linux/sysroot_scripts/install-sysroot.py”, line 157, in main
      subprocess.check_call([‘curl’, ‘–fail’, ‘-L’, url, ‘-o’, tarball])
      File “/usr/lib/python2.7/subprocess.py”, line 536, in check_call
      retcode = call(*popenargs, **kwargs)
      File “/usr/lib/python2.7/subprocess.py”, line 523, in call
      return Popen(*popenargs, **kwargs).wait()
      File “/usr/lib/python2.7/subprocess.py”, line 711, in __init__
      errread, errwrite)
      File “/usr/lib/python2.7/subprocess.py”, line 1343, in _execute_child
      raise child_exception
      OSError: [Errno 2] No such file or directory
      Error: Command ‘/usr/bin/python sdk/build/linux/sysroot_scripts/install-sysroot.py –running-as-hook –arch i386’ returned non-zero exit status 1 in /home/marcinsydor/dart-sdk

      Do you have an idea what is wrong?

      PS.
      Should I get an email when you reply to my comment? I’m asking because I have not got anything to the last one.

      Marcin

Leave a Reply

Your email address will not be published. Required fields are marked *