Clang use ld default rather than use lld to link objects

I’m just build a clang for a risc-v target, here is my cmake command to build the llvm:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_INSTALL_PREFIX=../install -DLLVM_TARGETS_TO_BUILD="RISCV"\
 -DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-linux-gnu" \
 ../llvm

When I compiled a project that use this clang, it would use the system default ld rather than lld for clang lead to the following question:

/usr/local/bin/ld: unrecognised emulation mode: elf64lriscv
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu elf_l1om elf_k1om
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

Did anybody know how to fix this question? BTW, I’m using the llvmorg-16.0.3 release tag to build my project, thanks!

We can use -DLLVM_USE_LINKER=lld

After I add this flags, the clang still call the ld to compile my file for riscv target:

Here is the my cflags in my make file to compile the object:

cflags = -O3 -fstrict-aliasing --target=riscv64-unknown-elf -menable-experimental-extensions \
-march=rv64gcv --sysroot=/home/lwh/rvgcc/riscv64-unknown-elf --gcc-toolchain=/home/lwh/rvgcc \
-fvectorize -fslp-vectorize

and my cmake option to recompile my llvm:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_INSTALL_PREFIX=../install \
-DLLVM_TARGETS_TO_BUILD="RISCV" -DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-linux-gnu" \
-DLLVM_USE_LINKER="lld" \
../llvm  && make -j 10

There is a diiferent chain reactions that using clang, if I use clang to compile the file directly to the binary, clang would call the riscv64-unknown-elf-ld, which can sucessfully build a riscv target. Howerver, when I split this process like:

~/llvm-project/build/bin/clang  --target=riscv64-unknown-elf\
 -menable-experimental-extensions\
 -march=rv64gcv --sysroot=/home/lwh/rvgcc/riscv64-unknown-elf\
 --gcc-toolchain=/home/lwh/rvgcc -c ./hello.c -o  hello 

~/llvm-project/build/bin/clang  ./hello.o -o  hello 

and this second step would cause the call to /usr/local/bin/ld,

In my Makefile step, it would break chain for this situation, leading to the call for ld,
may be change in makefile can rectify this question.

Try configuring with -DCLANG_DEFAULT_LINKER=lld

I finally figure out the problem, in the makefile, I don’t use the LDFLAGS containing the path to my gcc toolchain, this will lead clang fall back to call the defalut linker ld, so after I adding those flags in Makefile, problem solved.