llvm/clang的在Mac OS上的安装与使用

在Mac OS上的安装

通过命令:
brew install llvm

llvm会被安装在/usr/local/opt/llvm的路径下。
打开~/.bash_profile文件,添加环境变量:
export PATH="/usr/local/opt/llvm/bin:$PATH"
使用source ~/.bash_profile使.bash_profile文件文件生效

输入llvm-dis --version测试

llvm工具链的使用

创建一个C语言文件test.c

#include <stdio.h>
int main() {
  printf("hello world\n");
  return 0;
}

编译生成可执行文件:
clang test.c -o test

运行可执行文件:
test

生成llvm字节码文件:
clang -O1 -emit-llvm test.c -c -o test.bc

生成LLVM 的汇编代码 .ll 文件(可视化字节码文件)
clang -O1 -emit-llvm test.c -S -o test.ll

运行字节码文件:
lli test.bc
结果: "hello world"
.ll文件也可以用lli来执行

将 .bc 文件转化为 .ll 文件:
llvm-dis test.bc

将 .ll 文件转化为 .bc 文件:
llvm-as test.ll

编译字节码文件为汇编文件:
llc test.bc -o test.s

将 .bc 或 .ll 文件转化为本机平台的汇编代码:
llc test.bc
llc test.ll

参考链接:
https://blog.konghy.cn/2015/08/20/llvm-abstruct/
http://www.nagain.com/activity/article/4/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容