Cross compilation and sysroot

Hello,

I am trying to cross compile simple program using aarch64-linux-gnu-gcc on my amd64 machine running Fedora 40.

The program:

#include <limits.h>
#include <stdio.h>

int main(void) {
	printf("%d\n", PATH_MAX);
	return 0;
}

I believe in order to make this work one must provide sysroot.

I tried two sysroots:

In both cases I got the error is the same:

aarch64-linux-gnu-gcc main.c --sysroot=/tmp/tmp.cpRt4W1gBU/sysroot -c                                                                                                                                                                                                                              1 
main.c: In function ‘main’:
main.c:6:24: error: ‘PATH_MAX’ undeclared (first use in this function)
    6 |         printf("%d\n", PATH_MAX);
      |                        ^~~~~~~~
main.c:6:24: note: each undeclared identifier is reported only once for each function it appears in

This error means that the sysroot has some effect otherwise the error would say that even stdio.h does not exit.
However the error is weird – the macro is defined in one of the header files.

Even stranger is that if I use include dir flag -I instead of --sysroot the compilation passes!

aarch64-linux-gnu-gcc main.c -c -I/tmp/tmp.cpRt4W1gBU/sysroot/usr/include # this works!
aarch64-linux-gnu-gcc --sysroot=/tmp/tmp.cpRt4W1gBU/sysroot main.o # but I must split complation and linkage
QEMU_LD_PREFIX=/tmp/tmp.cpRt4W1gBU/sysroot ./a.out

Why is this happening? Is this a bug?