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:
Create a folder named raspberrypi in your home directory:
mkdir ~/raspberrypi
Navigate to this folder and clone the toolchain repository:
cd ~/raspberrypi git clone git://github.com/raspberrypi/tools.git
Integrating the Toolchain:
Access the desired toolchain:
export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
Update your terminal or restart your session:
Configuring CMake:
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):
Create a rootfs folder:
mkdir ~/raspberrypi/rootfs
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:
Clone the "cmake-hello-world" repository:
git clone https://github.com/jameskbride/cmake-hello-world.git
Create a build directory and navigate to it:
cd cmake-hello-world mkdir build cd build
Configure CMake using the toolchain file:
cmake -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake ../
Build the program:
make
Transfer the executable to your Raspberry Pi:
scp CMakeHelloWorld [email protected]:/home/pi/
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.
The above is the detailed content of How to Cross-Compile for Raspberry Pi on Ubuntu: Why Can't I Find libstdc ?. For more information, please follow other related articles on the PHP Chinese website!