问题描述
我目前在 ubuntu 上使用 clang11 来编译任何 c/c++ 代码并且它工作正常但是当我尝试编译任何代码时(包括 任何标准库)到任何特定目标的汇编代码,例如 x86_64
(即使我有 x86_64)riscv
并给出一个标记 --target=x86_64
或 --target=riscv32
我在代码中包含的任何标准库都出错了.一个简单的例子:
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
使用 --target=riscv32
标志:
clang++-11 --target=riscv32 -S ex.cpp -o ex.s
给出了这个错误:ex.cpp:1:9: 致命错误:找不到iostream"文件
即使我给出了一个特定的目标,也没有标准库也不会出错.
also without standard libraries gives no error even I give a spesific target.
我几天来一直在寻找解决方案,但我找不到任何合适的解决方案,他们中的大多数人说尝试包含 gnu 库和子文件夹,例如 -I/usr/include/x86_64-linux-gnu/c++/
但它对我不起作用.
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.
请不要说使用 g++ 编译器,为了添加优化我需要 clang.
Please don't say use g++ compiler, for adding an optimization I need clang.
实际上,我正在尝试为 riscv 目标编译我的代码,与 g++ 链接并使用spike 运行(没有区别 --target=...
或 -target ...
):
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?
或
我可以直接从 riscv bin utils 中使用 clang,比如 ~/riscv/bin/riscv32-unknown-elf-clang++
(我在网上看到了类似的东西但找不到)添加和为我的 riscv 目录构建一个子模块?
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?
正如@NateEldredge 所说,对于 x86_64 目标三元组应该 --target=x86_64-linux-gnu
但对于 riscv 作为目标三元组 riscv32-unknown-elf
我仍然有同样的错误.除了 --target=riscv32-unknown-elf
之外,还有其他适合 riscv 的目标标志吗?也许我错过了这一点.
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.
推荐答案
我通过使用 riscv-gnu-toolchain 在这里构建并详细回答了类似的问题:使用 Clang 为 RISC-V 编译
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
我们只需要交叉编译
.您也可以在这里查看更多信息:https://github.com/lowRISC/riscv-llvm#how-can-i-build-upstream-llvmclang-and-use-it-to-交叉编译for-a-riscv32-target
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
这篇关于当我给出一个特定的目标时,Clang 编译器找不到 C/C++ 标准库,尽管它在没有给出目标的情况下也能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!