3

I've been following instructions to cross compile code for the Raspberry Pi but I need some clarification regarding the tool chain and sysroot.

My setup is as follows:
Host: 4.15.0-76-generic x86_64
Target Pi: 4.19.93+ armv6l

I created a directory and downloaded the toolchain as follows:

$ mkdir rpi-cross
$ cd rpi-cross
$ git clone https://github.com/raspberrypi/tools

Within rpi-cross I created a sysroot directory and then used rsync to copy files from the target.

$ mkdir sysroot
$ rsync -avz pi@raspberrypi_ip:/lib sysroot
$ rsync -avz pi@raspberrypi_ip:/usr/include sysroot/usr
$ rsync -avz pi@raspberrypi_ip:/usr/lib sysroot/usr

I also repaired any absolute symlinks with relative ones as follows

$ wget https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
$ chmod +x sysroot-relativelinks.py
$ ./sysroot-relativelinks.py sysroot

I then exported the path to the toolchain and sysroot

export CROSS_COMPILE=/home/j/rpi-cross/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
export SYSROOT=/home/j/rpi-cross/sysroot

To test this I then created a simple hello-world application with the following make file

CC=$(CROSS_COMPILE)gcc

hello-world : main.c
    $(CC) --sysroot=$(SYSROOT) -march=armv6 -mfpu=vfp -mfloat-abi=hard -o hello-world main.c

But I got the following output:

In file included from main.c:1:0:
/home/j/rpi-cross/sysroot/usr/include/stdio.h:27:36: fatal error: bits/libc-header-start.h: No such file or directory
 #include <bits/libc-header-start.h>

As an additional test I made an even simpler main.c

//#include <stdio.h>
int main (int argc, char ** argv)
{
//    printf("Hello, world \r\n");
    return 0;
}

Which gave a different set of errors

/home/j/rpi-cross/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find crt1.o: No such file or directory
/home/j/rpi-cross/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find crti.o: No such file or directory

I then edited the makefile as follows, removing the sysroot option, and put the call to printf back into main.c.

CC=$(CROSS_COMPILE)gcc

hello-world : main.c
    $(CC) -march=armv6 -mfpu=vfp -mfloat-abi=hard -o hello-world main.c 

At which point the build was succesful and after copying the application onto the target I was able to run it.

$ file hello-world 
hello-world: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 2.6.32, not stripped

Further investigation reveals that there is a sysroot folder under /home/james/rpi-cross/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/arm-linux-gnueabihf/ which I assume the toolchain uses by default. This brings me to the crux of the question; what is wrong with the sysroot that I created by copying files from my target device? Is the best approch to use the sysroot that comes as part of the toolchain?

1
  • I've run into a very similar issue as you have, and finding the correct sysroot turned out to be far from trivial. Look for a directory named sysroot under the folder you stated above. – Neppomuk Apr 9 at 21:05
0

I can't answer your question, but I've been trying to do the same thing, and removing the SYSROOT setting (despite everything else I've read saying it was needed) was the key I needed. I can now compile for pi using just the pi tools from github, using WSL from Visual Studio. This is my toolchain file:

# Define our host system
SET(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
# Define the cross compiler locations
set(tools /home/david/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)
set(CMAKE_STAGING_PREFIX /home/david/stage)

# Don't mess with the sysroot, despite every post saying you should
#set(CMAKE_SYSROOT ${tools}/arm-linux-gnueabihf)

# Use our definitions for compiler tools
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries and headers in the target directories only
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.