Clang交叉编译arm-none-eabi目标时无法正确识别ARM GNU Toolchain的问题求助
Let's break down the two main issues you're facing and fix them step by step:
1. Linker Error: Missing ld.lld
Clang defaults to using LLVM's lld linker for cross-compilation, but your ARM GNU Toolchain only includes GNU's ld.bfd linker. To resolve this, explicitly tell Clang to use ld.bfd with the -fuse-ld=bfd flag.
2. Incorrect Header Search Paths
Clang is looking for libc++ (LLVM's standard library) headers at include/c++/v1, but your ARM Toolchain provides GCC's libstdc++ headers at include/c++/15.2.1. You need to:
- Switch Clang to use libstdc++ instead of libc++ with
-stdlib=libstdc++ - Fix the
--gcc-toolchainparameter: it should point to the root directory of your ARM toolchain (not the sysroot subdirectory), which helps Clang locate GCC's standard libraries and tooling correctly.
Working Compilation Command
Putting it all together, here's the corrected command:
clang --verbose \ --target=arm-none-eabi \ --gcc-toolchain=${HOME}/Desktop/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi \ --sysroot=${HOME}/Desktop/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/arm-none-eabi \ -fuse-ld=bfd \ -stdlib=libstdc++ \ -o executable \ main.cpp
Why This Works
--gcc-toolchain: Tells Clang where to find the ARM GCC toolchain's auxiliary files (like libstdc++ headers and runtime libraries). This parameter was being ignored before because you paired it with a sysroot path but didn't switch to libstdc++.-stdlib=libstdc++: Forces Clang to search for GCC's standard library headers instead of libc++, so it will look forinclude/c++/15.2.1instead ofinclude/c++/v1.-fuse-ld=bfd: Directs Clang to use the GNU linker included in your toolchain instead of trying to invoke lld, which isn't present.
Bonus Tip
For real-world ARM embedded projects, you'll likely need to specify a target CPU (e.g., -mcpu=cortex-m4 for a Cortex-M4 core) or floating-point ABI (e.g., -mfloat-abi=hard if your core has an FPU). Add these flags as needed for your hardware.
内容的提问来源于stack exchange,提问作者NoodleCollie

