首頁 > 後端開發 > C++ > 如何在 Ubuntu 上交叉編譯 Raspberry Pi:為什麼找不到 libstdc ?

如何在 Ubuntu 上交叉編譯 Raspberry Pi:為什麼找不到 libstdc ?

Barbara Streisand
發布: 2024-11-15 15:15:03
原創
311 人瀏覽過

How to Cross-Compile for Raspberry Pi on Ubuntu:  Why Can't I Find libstdc++?

How to Install a Cross-Compiler Toolchain on Your Host for the Raspberry Pi

Scenario:

You are attempting to cross-compile code for the Raspberry Pi on an Ubuntu machine. However, after installing the pre-built toolchain, you are encountering issues finding the libstdc++ shared library and utilizing the toolchain conveniently.

Solution:

To install and use the cross-compiler toolchain effectively, follow these steps:

Prerequisites:

Install the following prerequisites:

apt-get install git rsync cmake libc6-i386 lib32z1 lib32stdc++6
登入後複製

Setting Up the Toolchain:

  1. Create a folder named raspberrypi in your home directory:

    mkdir ~/raspberrypi
    登入後複製
  2. Navigate to this folder and clone the toolchain repository:

    cd ~/raspberrypi
    git clone git://github.com/raspberrypi/tools.git
    登入後複製

Integrating the Toolchain:

  1. Access the desired toolchain:

    export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
    登入後複製
  2. Update your terminal or restart your session:

    • Log out and log back in.
    • Run . ~/.bashrc in your terminal to refresh your PATH.

Configuring CMake:

  1. Create a CMake configuration file (~/raspberrypi/pi.cmake):

    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)
    登入後複製

Creating a File System Mirror (Optional):

  1. Create a rootfs folder:

    mkdir ~/raspberrypi/rootfs
    登入後複製
  2. Copy the /lib and /usr directories from your Raspberry Pi to ~/raspberrypi/rootfs:

    rsync -rl --delete-after --safe-links [email protected]:/{lib,usr} $HOME/raspberrypi/rootfs
    登入後複製

Cross-Compiling with CMake:

To cross-compile using your configured toolchain, use the -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake flag with CMake.

Example:

Compile a simple "Hello World" program for the Raspberry Pi:

  1. Clone the "cmake-hello-world" repository:

    git clone https://github.com/jameskbride/cmake-hello-world.git 
    登入後複製
  2. Create a build directory and navigate to it:

    cd cmake-hello-world
    mkdir build
    cd build
    登入後複製
  3. Configure CMake using the toolchain file:

    cmake -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake ../
    登入後複製
  4. Build the program:

    make
    登入後複製
  5. Transfer the executable to your Raspberry Pi:

    scp CMakeHelloWorld [email protected]:/home/pi/
    登入後複製
  6. Run the program on your Raspberry Pi:

    ssh [email protected] ./CMakeHelloWorld
    登入後複製

By following these steps, you will have successfully installed and integrated the Raspberry Pi cross-compiler toolchain, enabling you to conveniently cross-compile your applications.

以上是如何在 Ubuntu 上交叉編譯 Raspberry Pi:為什麼找不到 libstdc ?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板