LoginSignup
35
36

More than 5 years have passed since last update.

UbuntuにRaspberry Pi向けのC++クロスビルド環境をつくる

Posted at

環境

・Ubuntu14.04 x86(ホスト)

手順

クロスコンパイルツールのダウンロード

Raspberry Piのリポジトリからからクロスコンパイルツールをダウンロードする。

$ cd ~/raspi-dev/opt
$ git clone https://github.com/raspberrypi/tools.git

特にビルドなどはせずに使える。

$ ./tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ --version
arm-linux-gnueabihf-g++ (crosstool-NG linaro-1.13.1-4.8-2014.01 - Linaro GCC 2013.11) 4.8.3 20140106 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Raspberry Piへのg++4.8のインストール

ダウンロードしたツールでコンパイルしたバイナリをRaspberry Piで実行すると次のようなエラーが出た。

/usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version `GLIBCXX_3.4.18' not found (required by ./hello)

11.04 - GLIBCXX_3.4.15 not found - Ask Ubuntuに書いてあるやり方で調べたら確かにない。

# Raspberry Pi
$ sudo find / -name libstdc++.so
/usr/lib/gcc/arm-linux-gnueabihf/4.6/libstdc++.so
/usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.17
/usr/lib/arm-linux-gnueabihf/libstdc++.so.6

$ strings /usr/lib/gcc/arm-linux-gnueabihf/4.6/libstdc++.so | grep GLIBC
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBC_2.4
GLIBCXX_DEBUG_MESSAGE_LENGTH

クロスコンパイラのg++のバージョンが4.8でRaspberry Piにインストールされているg++のバージョンが4.6だから起きるようなので、Raspberry Piにg++4.8を入れることにした。

インストールはこのページを参考にした。
ただし今回は新しいlibstdc++が入れば良いので、g++インストール後のupdate-alternativesは行っていない。

gcc 4.8 on Raspberry Pi Wheezy | some wide open space

まとめ

これで実行できるようになった。

# [Ubuntu]
$ cat hello.cpp 
#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << "hello world" << std::endl;
    return 0;
}

$ export PATH=$PATH:~/raspi-dev/opt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/
$ arm-linux-gnueabihf-g++ hello.cpp -o hello
$ rsync ./hello pi@xxx.xxx.xxx.xxx:~/

# [Raspberry Pi]
$ cd ~
$ ./hello 
hello world
35
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
36