clinuxlinkerclangcross-compiling

Cross compiling with Clang - `crtbeginS.o: No such file or directory`


I'm trying to cross compile a C file to an aarch64 ELF on x86 linux. I've set up a cross build environment on Gentoo but when I run the following command:

clang -target aarch64-unknown-linux-gnu --sysroot=/usr/aarch64-unknown-linux-gnu/ input.c -I /usr/lib/gcc/aarch64-unknown-linux-gnu/11.3.0/ -L /usr/lib/gcc/aarch64-unknown-linux-gnu/11.3.0/

I get the following error:

/usr/bin/aarch64-unknown-linux-gnu-ld: cannot find crtbeginS.o: No such file or directory

However, in the directory /usr/lib/gcc/aarch64-unknown-linux-gnu/11.3.0/, there is a file called crtbeginS.o. My question then is what additional compiler flags am I missing to get the linker to find this file?


Solution

  • clang doesn't seem to pick up these 2 files in particular from their original location, even if you point to them explicitly. You can instead copy or symlink the files in a directory that will cause them to be picked up, like the $SYSROOT/usr/lib/ directory. Adjusting for the paths in your post:

      ln -s /usr/lib/gcc/aarch64-unknown-linux-gnu/11.3.0/crtbeginS.o /usr/aarch64-unknown-linux-gnu/usr/lib/crtbeginS.o
      ln -s /usr/lib/gcc/aarch64-unknown-linux-gnu/11.3.0/crtendS.o /usr/aarch64-unknown-linux-gnu/usr/lib/crtendS.o
    

    (this was tested with clang-16.0.1)