How to specify the default runtime library for clang links?

I am building a riscv application development environment based on llvm. Build clang using the following cmake command:

cmake -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${path}/llvm_host \
-DLLVM_TARGETS_TO_BUILD="RISCV" \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_USE_LINKER=gold \
-DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-linux-gnu" \
../llvm

Then compile 64-bit glibc using the following command based on the official riscv-gnu-toolchain source:

../configure --prefix=${path}/glibc64_host
make linux -j8

Finally, compile the “test.cpp” using clang and glibc64:

// file test.cpp
#include<stdio.h>
int main(int argc, char *argv[]){
  printf("hello\n");
  return 0;
}
${path}/llvm_host/bin/clang --target=riscv64 -march=rv64imafcv \
--sysroot=${path}/glibc64_host/sysroot \
--gcc-toolchain=${path}/glibc64_host \
-g -fuse-ld=lld test.cpp -o test

However, clang complains that it can’t find ‘stdio.h’ :

test.cpp:1:9: fatal error: 'stdio.h' file not found

Furthermore, when clang is given the path of “stdio.h” using the -I command, it further complains that the runtime library is not found:

ld.lld: error: cannot open crt0.o: No such file or directory
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lgloss

Notice that “crt0.o” is newlib’s launch library function, not glibc’s. If I compile test.cpp against newlib, everything is fine.

${path}/llvm_host/bin/clang --target=riscv64 -march=rv64imafcv \
--sysroot=${path}/newlib64_host/sysroot \
--gcc-toolchain=${path}/newlib64_host \
-g -fuse-ld=lld test.cpp -o test

It looks like the default runtime library for clang is newlib. What can I do to get clang to treat glibc as a runtime library , so that it looks for the relevant headers and links to the correct launcher functions, the way glibc works?

Specifically, I’m not sure what cmake options should be configured to build clang, and just changing 'CMAKE_CXX_FLAGS 'and 'CMAKE_EXE_LINKER_FLAGS 'doesn’t seem to fix the root cause of the problem.

Another problem was that I didn’t know how to view the cmake options of the llvm source code. I tried to view the cmake options with the following command:

$user:/home/user/llvm-project/build$ cmake -LAH -DCMAKE_MODULE_PATH=../cmake/Modules ../

However, cmake keeps telling me that the cmakelists.txt file in the target directory doesn’t exist!

CMake Error: The source directory "/home/user/work/llvm-project/" does not appear to contain CMakeLists.txt.

I know llvm uses cmake modules to perform cmake builds, but the argument ‘DCMAKE_MODULE_PATH’ doesn’t seem to get this message to cmake.

I don’t know how to get clang to compile with glibc as runtime library, and the clang version is 14.0.6. Any suggestions would be helpful, thanks!

–target=riscv64 means riscv64-unknown-elf (this is true of all triples), hence why it’s looking for newlib not glibc. If you want a GNU/Linux target, use --target=riscv64-linux-gnu (or nothing, given you specified the default correctly in your cmake invocation).

1 Like

Thanks!! It works. But I still want to ask how to view llvm configurable options via cmake tool?

Because it’s ../llvm, like you managed to correctly use for actually building it.

Thanks!