c++ - 使用 Clang 和 LLVM

标签 c++ clang llvm clang++ llc

我正在编译这个:

int main(){
}

使用 clang,使用这个命令行:

clang++.exe -S -o %OUTFILE%.clang -emit-llvm %INFILE% -I. -I%INCLUDE_PATH% -std=c++14 -ftemplate-depth=1000

这给了我 llvm 字节码。

然后我像这样使用 llc 将字节码转换为 C 代码:

llc "%IN_FILE%.clang" -march=c -o foo.c

并得到这个错误:

error: unterminated attribute group
attributes #0 = { norecurse nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

我做错了什么?

这就是 clang++ 给我的:

; ModuleID = 'C:\Users\Owner\Documents\C++\SVN\prototypeInd\util\StaticBigInt.cpp'
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc18.0.0"

; Function Attrs: norecurse nounwind readnone uwtable
define i32 @main() #0 {
entry:
  ret i32 0
}

attributes #0 = { norecurse nounwind readnone uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"clang version 3.8.0 (branches/release_38)"}

注意: 我正在使用 clang 3.8 版和 llc 3.4 版

最佳答案

当您运行如下命令时:

clang -S -emit-llvm ...

然后编译器发出的不是位码形式的 IR,而是人类可读的表示形式。

如果您使用的所有工具都具有相同的版本,或者您只想手动检查输出,这是有意义的。

但是,人类可读的 IR 可能与旧工具不兼容。

在这种情况下我可以推荐直接使用bitcode(注意不再有-S参数):

clang -emit-llvm

关于c++ - 使用 Clang 和 LLVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634340/

相关文章:

c++ - CL_INVALID_KERNEL_NAME : when Struct in Kernel (Opencl)

c++ - 如何通过 Clang 和 LibTooling 使用标准库

c++ - 如果语句包含宏,则 clang 无法替换语句

c++ - 使用 LLVM IR 实现动态类型语言

c++ - LLVM传递计数 vector 类型指令

c++ - Xcode 8.3.1 - 编译器不能再处理循环引用?

c++ - 如何在应用程序退出时诊断访问冲突

java - 什么是最好的视频处理编程语言?

clang - 为什么 clang -dumpversion 报告 4.2.1

c++ - RSA 公钥加密 openssl