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?
sysroot
under the folder you stated above.