clang编译器简介

本文部分内容引用: + 中文维基百科。 + 结构化编译器前端--clang介绍

什么是clang编译器?

clang是LLVM编译器工具集的一个用于编译C、C++、Objective-C的前端。LLVM项目的目标是提供一个GNU编译器套装(gcc)的替代品,由苹果公司的赞助开发,其源代码授权采用的是类BSD的伊利诺伊大学厄巴纳-香槟分校开源码许可。

clang编译器的优势与劣势

相比于gcc,clang具有如下优点:

当前 Clang 还处在不断完善过程中,相比于gcc, clang在以下方面还需要加强:

安装LLVM + clang

二进制安装

源码安装

使用clang编译C程序

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”说明编译器已经可以正常工作。 cygwin