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

查看:75
本文介绍了如何在我的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.

注意: 本教程仅适用于较旧的树莓派图像.对于基于Debian Buster的较新Raspbian,请在该线程中查看以下操作方法: https://stackoverflow.com/a/58559140/869402

NOTE: This tutorial only works for older raspbian images. For the newer Raspbian based on Debian Buster see the following how-to in this thread: https://stackoverflow.com/a/58559140/869402

开始之前,需要确保已安装以下组件:

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

apt-get install git rsync cmake ia32-libs

让我们交叉编译一个Pie!

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

进入此文件夹,然后下拉您上面提到的ENTIRE tools文件夹:

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

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

如果我没看错的话,您想使用以下三个中的一个,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天全站免登陆