本文部分内容引用: + 中文维基百科。 + 结构化编译器前端--clang介绍。
本文部分内容引用: + 中文维基百科。 + 结构化编译器前端--clang介绍。
clang是LLVM编译器工具集的一个用于编译C、C++、Objective-C的前端。LLVM项目的目标是提供一个GNU编译器套装(gcc)的替代品,由苹果公司的赞助开发,其源代码授权采用的是类BSD的伊利诺伊大学厄巴纳-香槟分校开源码许可。
相比于gcc,clang具有如下优点:
当前 Clang 还处在不断完善过程中,相比于gcc, clang在以下方面还需要加强:
apt-cyg install clang
sudo yum install clang
sudo apt-get install clang-3.4 clang-3.4-doc libclang-common-3.4-dev libclang-3.4-devlibclang1-3.4 libclang1-3.4-dbg libllvm-3.4-ocaml-dev libllvm3.4 libllvm3.4-dbg lldb-3.4 llvm-3.4 llvm-3.4-dev llvm-3.4-doc llvm-3.4-examples llvm-3.4-runtime clang-modernize-3.4 clang-format-3.4 python-clang-3.4 lldb-3.4-dev
第一步,下载llvm代码:
git clone git@github.com:llvm-mirror/llvm.git
第二步,进入llvm/tools目录并下载clang代码
cd llvm/tools
git clone git@github.com:llvm-mirror/clang.git
cd ../projects
git clone git@github.com:llvm-mirror/compiler-rt.git
cd ../..
mkdir build
cd build
../llvm/configure --enable-optimized --enable-assertions
make
make install
clang的用法与gcc基本相同,我们可以写一个脚本来验证一下编译器是否已经安装完成:
import os
import sys
import shutil
if not len(sys.argv) in range(2, 3):
print("Usage: hello_c.py <compiler>")
exit(1)
code = "#include <stdio.h>\n int main(void) { printf(\"hello world!\\n\"); return 0;} "
if(not os.path.exists("example")):
os.mkdir("example")
file = open(r"example/hello.c",'w')
file.writelines(code)
file.close()
cmd = sys.argv[1] + r" example/hello.c -o example/test.exe"
os.system(cmd)
os.system(r"example/test.exe")
if(os.path.exists("example")):
shutil.rmtree("example")
然后,我们只需要在shell中输入python hello_c.py clang
即可,如果看到输出一行“hello world”说明编译器已经可以正常工作。