Today, we’re going to build our own toolchain using crosstool-NG. There are so many cases that we want to build a particular toolchain for a specific embedded system development board. One MOST important reason for that is probably because the specific development board has limited resource to build upon, therefore, the time-consumption to build some software on the development board is VERY inefficient. In the other way around, cross compiling on the host PC is MORE efficient with respective to the time-consumption.
In this blog, for simplicity, we take Raspberry Pi 3B as our demo development board, for which we are building the cross compiler. Some references can be found at:
How to install crosstool-NG are thoroughly summarized at its official website. In my case, I had it installed under folder /opt/compilers/crosstool-ng. Let’s take a look:
1 2 3 4 5 6
longervision-GT72-6QE% pwd /opt/compilers/crosstool-ng longervision-GT72-6QE% ls bin libexec share longervision-GT72-6QE% ls bin ct-ng
And make sure /opt/compilers/crosstool-ng/bin is under environment variable PATH.
2. Configuration
Under any directory that you want to save your .config file, we can configure our target cross compiler.
➜ ~ uname -a Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux
Binary utilities:
Binary format: ELF
Version of binutils: 2.31.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
➜ ~ apt show binutils Package: binutils Version: 2.31.1-16+rpi1 Priority: optional Section: devel Maintainer: Matthias Klose <doko@debian.org> Installed-Size: 95.2 kB Provides: binutils-gold, elf-binutils Depends: binutils-common (= 2.31.1-16+rpi1), libbinutils (= 2.31.1-16+rpi1), binutils-arm-linux-gnueabihf (= 2.31.1-16+rpi1) Suggests: binutils-doc (>= 2.31.1-16+rpi1) Conflicts: binutils-multiarch (<< 2.27-8), modutils (<< 2.4.19-1) Homepage: https://www.gnu.org/software/binutils/ Download-Size: 56.9 kB APT-Manual-Installed: no APT-Sources: http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages Description: GNU assembler, linker and binary utilities The programs in this package are used to assemble, link and manipulate binary and object files. They may be used in conjunction with a compiler and various libraries to build programs.
➜ ~ apt show gcc Package: gcc Version: 4:8.3.0-1+rpi2 Priority: optional Section: devel Source: gcc-defaults (1.181+rpi2) Maintainer: Debian GCC Maintainers <debian-gcc@lists.debian.org> Installed-Size: 46.1 kB Provides: c-compiler, gcc-arm-linux-gnueabihf (= 4:8.3.0-1+rpi2) Depends: cpp (= 4:8.3.0-1+rpi2), gcc-8 (>= 8.3.0-1~) Recommends: libc6-dev | libc-dev Suggests: gcc-multilib, make, manpages-dev, autoconf, automake, libtool, flex, bison, gdb, gcc-doc Conflicts: gcc-doc (<< 1:2.95.3) Download-Size: 5,200 B APT-Manual-Installed: no APT-Sources: http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages Description: GNU C compiler This is the GNU C compiler, a fairly portable optimizing compiler for C. . This is a dependency package providing the default GNU C compiler.
After we save the configuration to file .config, we Exit the crosstool-NG configuration dialog.
3. Build
Please remember to:
unset LD_LIBRARY_PATH before building. Otherwise, you’ll meet some ERROR messages.
mkdir ~/src before building. Otherwise, whenever you tried to rerun ct-ng build, you’ll have to download ALL required packages from scratch.
Finally, let’s take a look at the version of our built cross compilers for Raspberry Pi 3B.
1 2 3 4 5 6 7 8 9 10 11 12 13
longervision-GT72-6QE% arm-rpi-linux-gnueabihf-gcc --version zsh: command not found: arm-rpi-linux-gnueabihf-gcc longervision-GT72-6QE% ./arm-rpi-linux-gnueabihf-gcc --version arm-rpi-linux-gnueabihf-gcc (crosstool-NG 1.24.0.6-afaf7b9) 8.3.0 Copyright (C) 2018 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. longervision-GT72-6QE% ./arm-rpi-linux-gnueabihf-g++ --version arm-rpi-linux-gnueabihf-g++ (crosstool-NG 1.24.0.6-afaf7b9) 8.3.0 Copyright (C) 2018 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.
CMAKE_SYSROOT specify the sysroot directory which emulate your target environment, here, Raspberry Pi 3B
CMAKE_STAGING_PREFIX is where the STAGING results store, for the reason that final results may require to be built in multiple stages. You may refer to Linux From Scratch for further background knowledge about that.
tools actually specify the building tool directory. Making sure all generated cross compiling tools are under folder ${tools}/bin.
4.1.2 Ignore hdf5
In addition, for the emulated Raspberry Pi 3B sysroot, hdf5 is NOT supported. Therefore, let’s simply comment out the following line in flann CMakeLists.txt.
....../flann/src/cpp/flann/algorithms/autotuned_index.h: In member function 'void flann::AutotunedIndex<Distance>::optimizeKMeans(std::vector<flann::AutotunedIndex<Distance>::CostData>&) [with Distance = flann::L2<double>]': ....../flann/src/cpp/flann/algorithms/autotuned_index.h:379:31: note: parameter passing for argument of type 'flann::L2<double>' changed in GCC 7.1 KMeansIndex<Distance> kmeans(sampledDataset_, cost.params, distance_); ^~~~~~ [ 75%] Linking CXX static library ../../lib/libflann_s.a cd ....../flann/build/src/cpp && /usr/bin/cmake -P CMakeFiles/flann_s.dir/cmake_clean_target.cmake cd ....../flann/build/src/cpp && /usr/bin/cmake -E cmake_link_script CMakeFiles/flann_s.dir/link.txt --verbose=1 /opt/compilers/RPi/bin/arm-rpi-linux-gnueabihf-ar qc ../../lib/libflann_s.a CMakeFiles/flann_s.dir/flann/flann.cpp.o /opt/compilers/RPi/bin/arm-rpi-linux-gnueabihf-ranlib ../../lib/libflann_s.a make[2]: Leaving directory '....../flann/build' [ 75%] Built target flann_s make -f src/cpp/CMakeFiles/flann.dir/build.make src/cpp/CMakeFiles/flann.dir/depend make[2]: Entering directory '....../flann/build' cd ....../flann/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" ....../flann ....../flann/src/cpp ....../flann/build ....../flann/build/src/cpp ....../flann/build/src/cpp/CMakeFiles/flann.dir/DependInfo.cmake --color= Scanning dependencies of target flann make[2]: Leaving directory '....../flann/build' make -f src/cpp/CMakeFiles/flann.dir/build.make src/cpp/CMakeFiles/flann.dir/build make[2]: Entering directory '....../flann/build' [ 87%] Building CXX object src/cpp/CMakeFiles/flann.dir/empty.cpp.o cd ....../flann/build/src/cpp && /opt/compilers/RPi/bin/arm-rpi-linux-gnueabihf-g++ --sysroot=....../RPi/arm-rpi-linux-gnueabihf/sysroot -DFLANN_EXPORTS -D_FLANN_VERSION=1.9.1 -I....../flann/src/cpp -I/usr/include -O2 -g -DNDEBUG -fPIC -Wall -Wno-unknown-pragmas -Wno-unused-function -o CMakeFiles/flann.dir/empty.cpp.o -c ....../flann/src/cpp/empty.cpp [100%] Linking CXX shared library ../../lib/libflann.so cd ....../flann/build/src/cpp && /usr/bin/cmake -E cmake_link_script CMakeFiles/flann.dir/link.txt --verbose=1 /opt/compilers/RPi/bin/arm-rpi-linux-gnueabihf-g++ --sysroot=....../RPi/arm-rpi-linux-gnueabihf/sysroot -fPIC -I/usr/include -O2 -g -DNDEBUG -shared -Wl,-soname,libflann.so.1.9 -o ../../lib/libflann.so.1.9.1 CMakeFiles/flann.dir/empty.cpp.o -Wl,-whole-archive ../../lib/libflann_s.a -Wl,-no-whole-archive -Wl,--push-state,--no-as-needed -Wl,--pop-state cd ....../flann/build/src/cpp && /usr/bin/cmake -E cmake_symlink_library ../../lib/libflann.so.1.9.1 ../../lib/libflann.so.1.9 ../../lib/libflann.so make[2]: Leaving directory '....../flann/build' [100%] Built target flann make[1]: Leaving directory '....../flann/build' /usr/bin/cmake -E cmake_progress_start ....../flann/build/CMakeFiles 0
You can now:
make install to install the built/generated libraries installed under CMAKE_INSTALL_PREFIX
copy and paste the built/generated libraries onto Raspberry Pi 3B and use it directly.
BTW: do NOT forget to install the header files.
5. Additional Issues
Multilib/multiarch seems to be problematic nowadays. Please pay attention to Multilib/multiarch. Some related issues are enumuated as the end of this blog.