Clang compiler couldn't find C/C++ standard libraries when I gave a specific target although it works without giving target
Asked Answered
T

1

0

I am currently using clang11 on ubuntu to compile any c/c++ code and it works fine but when I tried to compile any code (including any standard library) to assembly code for any specific target like x86_64 (even I have x86_64) riscv with giving a flag that --target=x86_64 or --target=riscv32 I got errors for any standard library that I included in my code. A simple example:

// ex.cpp
#include<iostream>
int main(){
    int a = 5;
    int b = 3;
    std::cout << a - b;
}

Without giving flag for a spesific target works fine:

clang++-11 -S ex.cpp -o ex.s

With --target=riscv32 flag:

clang++-11 --target=riscv32 -S ex.cpp -o ex.s

gives this error: ex.cpp:1:9: fatal error: 'iostream' file not found

also without standard libraries gives no error even I give a spesific target.

I am searching for a solution for days but I couldn't find any proper solution for this problem, most of them says try to include gnu libraries and subfolders like -I/usr/include/x86_64-linux-gnu/c++/ but it doesn't work for me.

Please don't say use g++ compiler, for adding an optimization I need clang.

Actually I am trying to compile my codes for riscv target, linking with g++ and running with spike (doesn't differ --target=... or -target ...):

clang++-11 -target riscv32-unknown-elf -march=rv32gc -fno-addrsig -S ex.cpp -o ex.s
~/riscv/bin/riscv32-unknown-elf-g++ ex.s -o ex
~/riscv/riscv-isa-sim/build/spike --isa=RV32GC ~/riscv/riscv-pk/build/pk ex

And it works fine without include a standard library. Now, I want to ask that

Can I solve this problem simply?

or

Can I use clang directly from riscv bin utils like ~/riscv/bin/riscv32-unknown-elf-clang++ (I saw something like this on the net but couldn't find) adding and building a submodule to my riscv directory?

Edit: As @NateEldredge said, for x86_64 target triple should --target=x86_64-linux-gnu but for riscv as a target triple riscv32-unknown-elf I still have the same errors. Is there a proper target flag for riscv any other than --target=riscv32-unknown-elf? Maybe I am missing that point.

Testa answered 19/2, 2021 at 22:34 Comment(11)
Are you compiling as C or C++? Is your code C or C++? They are distinct languages. For example, C++ has std::string and std::vector, which would be different libraries than C. Please update your tags and question about which language you are using.Florineflorio
You probably need to install heaers and libraries for the other targetsLeadbelly
@Leadbelly How can I install, especially for riscv? Also, as I said even I give x86_64 for target I get same error although I am working on x86_64 and also I can compile without no error without no target flag.Testa
@ThomasMatthews Sorry but as you can see, I just gave an example for C++, this is a general problem for me even if compiler is clang (for C) or clang++ (for C++) because of that I am asking general. For example, if I try to compile a C code with stdio.h library I get same error like fatal error: 'stdio.h' file not found and so forth for any other library.Testa
Shouldn't the argument to --target be a target triplet, not just an architecture? So --target=x86_64-linux-gnu instead of just x86_64. Running clang -v should show you what the default target triplet is.Hoeve
More generally, you can use the verbosity flag (-v), to see what libraries and include paths are implicitly used.Turmoil
@NateEldredge Right, I will edit my post, missed that thing target triple should --target=x86_64-linux-gnu and it works but it is weird that without including a library, codes are compiling with just like --target=x86_64 but this is not the case :) Actually I need specify a target because I don't use on my x86, I need for riscv and I tried for riscv as a target triple riscv32-unknown-elf or riscv32-none-elf and also tried for arm like arm-none-eabi but noone works. Probably a proper target triple will work but couldn't find for riscv just find riscv32-unknown-elf that doesnt work.Testa
How did you build and install your riscv32 cross compiler? Using --target with the installed clang binary will search for its files in a specific place, which maybe is not where you have them installed.Hoeve
OT but quite irritating: a header file is not a library. It may define parts or all of the public API of one or more libraries, but not necessarily. It can even be empty. -- A library might be needed later when linking, but you are just compiling. Please get used to the proper terms.Bulletproof
@NateEldredge Actually I didn't build a cross compiler firstly, just built llvm-project with -DLLVM_TARGETS_TO_BUILD=all option and used clang compiler from this built, because after llvm 9 i know llvm support came for riscv target. (All supported targets can be seen here.) I mean as llvm promised I should have been able to used clang for all targets with llvm in built but didn't work for me.Testa
I just solved my problem cross compilation with riscv-llvm repo which has not updated for a long time and is not official for now. I didn't understand that why llvm has a support for riscv but I couldn't use its clang compiler for this spesific target, why I need a cross compilation or if I need that how I can cross compile riscv gnu toolchain with official llvm.Testa
T
3

I solved my problem by linking compilations with riscv-gnu-toolchain built and also answered a similar question here in detailed: Using Clang to compile for RISC-V

Simply we need cross-compilation. Further information you can also look here: https://github.com/lowRISC/riscv-llvm#how-can-i-build-upstream-llvmclang-and-use-it-to-cross-compile-for-a-riscv32-target

Testa answered 29/7, 2021 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.