如何在我的 Linux 主机上安装 Raspberry Pi 交叉编译器? [英] How to install the Raspberry Pi cross compiler on my Linux host machine?

查看:23
本文介绍了如何在我的 Linux 主机上安装 Raspberry Pi 交叉编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为在我的 Ubuntu 机器上工作的 Raspberry Pi 进行交叉编译.

I am attempting to get cross-compiling for Raspberry Pi working on my Ubuntu machine.

在我最初的尝试中,我使用了 arm-linux-gnueabi 编译器,它可以在 Ubuntu 存储库中找到.我得到了这个工作.我能够构建所有依赖项并在我的 cmake 项目中使用交叉编译器.

During my initial attempts I was using the arm-linux-gnueabi compiler, which is available in the Ubuntu repo. I got this working. I was able to build all my dependencies and use the cross-compiler in my cmake project.

但是,我认为我应该使用 hf 版本,所以我切换到 arm-linux-gnueabihf.然后我意识到这不适用于 Raspberry Pi,因为它是 armv6.

However, I believe I should be using the hf version, so I switched to arm-linux-gnueabihf. Then I realized that this does not work with Raspberry Pi since it is armv6.

经过一番谷歌搜索,我找到了来自 GitHub 的预构建工具链.

After some Googling, I then found the pre-built toolchain from GitHub.

我下载了工具链,但我不太明白如何安装"它.我将文件提取到我的主目录.目录结构如下:

I downloaded the toolchain, but I don't really understand how to "install" it. I extracted the files to my home directory. The directory structure looks like this:

/gcc-linearo-arm-linux-gnueabihf-raspbian
    /arm-linux-gnueabihf
        /bin
            (contains g++, gcc, etc)
        /lib
            (contains libstdc++ library)
    /bin
        (contains arm-linux-gnueabihf-g++, arm-linux-gnueabihf-...)
    /lib
        (gcc lib stuff)

如果我将目录更改为 INNER bin 文件夹,我可以从终端编译测试程序而不会出现任何问题.

If I change directory to the INNER bin folder I am able to compile a test program from the terminal without any problems.

~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/
arm-linux-gnueabihf/bin$ g++ test.cpp -o test

然后我尝试在 OUTER bin 文件夹中编译一个测试程序,其中包含工具的前缀版本.

I then tried to compile a test program in the OUTER bin folder, which contains the prefixed versions of the tools.

 ~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin$ 
 arm-linux-gnueabihf-g++ test.cpp -o test

但是,当我现在尝试使用编译器时(从内部 bin 目录之外),它无法找到工具链附带的 libstdc++ 共享库:

However, when I try to use the compiler now (from outside the inner bin directory), it is unable to find the libstdc++ shared library that comes with the toolchain:

arm-linux-gnueabihf-gcc: error while loading shared libraries: 
libstdc++.so.6: cannot open shared object file: No such file or directory.

此外,我希望能够使用编译器而无需导航到 bin 目录.所以我尝试将 OUTER bin 目录(因为我想要前缀版本)和两个 lib 目录添加到我的 PATH:

Furthermore, I want to be able to use the compiler without having to navigate to the bin directory. So I tried adding the OUTER bin directory (since I want the prefixed versions) and both lib directories to my PATH:

export PATH=$PATH:~/tools/.../bin
export PATH=$PATH:~/tools/.../lib
export PATH=$PATH:~/tools/.../.../lib

但是,这会导致相同的错误.我应该如何安装"工具链,以便我可以在任何地方使用工具链,就像我在使用 Ubuntu 存储库中的交叉编译器时一样?

However, this results in the same error. How should I "install" the toolchain so that I can use the toolchain from everywhere, just like I can when I use the cross-compilers from the Ubuntu repo?

推荐答案

我会试着把它写成一个教程给你,这样它就容易上手了.

I'm gonna try to write this as a tutorial for you so it becomes easy to follow.

注意:本教程仅适用于较旧的 raspbian 图像.对于基于 Debian Buster 的较新的 Raspbian,请参阅此线程中的以下操作方法:https://stackoverflow.com/a/58559140/869402

在开始之前,您需要确保已安装以下内容:

Before you start you need to make sure the following is installed:

apt-get install git rsync cmake libc6-i386 lib32z1 lib32stdc++6

让我们交叉编译一个 Pie!

首先在您的主目录中创建一个名为 raspberrypi 的文件夹.

进入此文件夹并下拉您上面提到的整个工具文件夹:

Go in to this folder and pull down the ENTIRE tools folder you mentioned above:

git clone git://github.com/raspberrypi/tools.git

如果我没看错的话,你想使用以下 3 个,gcc-linaro-arm-linux-gnueabihf-raspbian.

You wanted to use the following of the 3 ones, gcc-linaro-arm-linux-gnueabihf-raspbian, if I did not read wrong.

进入你的主目录并添加:

Go into your home directory and add:

export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin

到名为 ~/.bashrc 的文件的末尾

现在您可以注销并重新登录(即重新启动终端会话),或运行 .~/.bashrc 在您的终端中获取 PATH 在您当前的终端会话中添加的内容.

Now you can either log out and log back in (i.e. restart your terminal session), or run . ~/.bashrc in your terminal to pick up the PATH addition in your current terminal session.

现在,验证您是否可以访问编译器 arm-linux-gnueabihf-gcc -v.你应该得到这样的东西:

Now, verify that you can access the compiler arm-linux-gnueabihf-gcc -v. You should get something like this:

Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/tudhalyas/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../libexec/gcc/arm-linux-gnueabihf/4.7.2/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: /cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.b
 uild/src/gcc-linaro-4.7-2012.08/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-
 linux-gnu --target=arm-linux-gnueabihf --prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-l
 inux-gnueabihf-raspbian-linux/install --with-sysroot=/cbuild/slaves/oort61/crosstool-ng/builds/
 arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fo
 rtran --disable-multilib --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=
 hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08' --with-bugurl=
 https://bugs.launchpad.net/gcc-linaro --enable-__cxa_atexit --enable-libmudflap --enable-libgom
 p --enable-libssp --with-gmp=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-rasp
 bian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/cbuild/slaves/oort61/crosstool-
 ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc
 =/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-
 gnueabihf/build/static --with-ppl=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf
 -raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/cbuild/slaves/oort61/cros
 stool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --wi
 th-libelf=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/a
 rm-linux-gnueabihf/build/static --with-host-libstdcxx='-L/cbuild/slaves/oort61/crosstool-ng/bui
 lds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static/lib -lpwl' --ena
 ble-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gol
 d --with-local-prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-li
 nux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08
 )

但是,嘿!我这样做了,但库仍然无法正常工作!

我们还没有完成!到目前为止,我们只完成了基础.

But hey! I did that and the libs still don't work!

We're not done yet! So far, we've only done the basics.

在您的 raspberrypi 文件夹中,创建一个名为 rootfs 的文件夹.

In your raspberrypi folder, make a folder called rootfs.

现在您需要将整个 /lib/usr 目录复制到这个新创建的文件夹中.我通常把 rpi 镜像打开并通过 rsync 复制它:

Now you need to copy the entire /liband /usr directory to this newly created folder. I usually bring the rpi image up and copy it via rsync:

rsync -rl --delete-after --safe-links pi@192.168.1.PI:/{lib,usr} $HOME/raspberrypi/rootfs

其中 192.168.1.PI 替换为您的 Raspberry Pi 的 IP.

where 192.168.1.PI is replaced by the IP of your Raspberry Pi.

现在,我们需要编写一个 cmake 配置文件.在您喜欢的编辑器中打开 ~/home/raspberrypi/pi.cmake 并插入以下内容:

Now, we need to write a cmake config file. Open ~/home/raspberrypi/pi.cmake in your favorite editor and insert the following:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspberrypi/rootfs)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

现在您应该能够编译您的 cmake 程序,只需添加以下额外标志:-D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake.

Now you should be able to compile your cmake programs simply by adding this extra flag: -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake.

使用 cmake hello world 示例:

git clone https://github.com/jameskbride/cmake-hello-world.git 
cd cmake-hello-world
mkdir build
cd build
cmake -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake ../
make
scp CMakeHelloWorld pi@192.168.1.PI:/home/pi/
ssh pi@192.168.1.PI ./CMakeHelloWorld

这篇关于如何在我的 Linux 主机上安装 Raspberry Pi 交叉编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆