天天看点

linux下clang+llvm3.3安装(ubuntu)

最近要用clang api进行一些简单的查错处理(往高里说叫Runtime Verification),结果配置环境弄了好久......(linux基础知识很重要.....makefile要学好, gcc要学好, 至少编译的时候多用用-###  -v , 看看库函数到底从哪里找到的, 理解.a  .so等文件类型 , 软连接, 多用locate(注意updatedb), 不要各种版本装得乱七八糟, 路径, 改动bash...)

终于能够正常运行里的代码了 https://github.com/loarabia/Clang-tutorial 

刚开始没仔细看llvm getting started           http://llvm.org/docs/GettingStarted.html

只是照着网上不全的教程看,出了一大堆问题,官方文档虽然好但也不能照抄,否则会装10+G的内容(Debug版且没有优化,我装的时候容量耗光了,还得扩展(在VM上))

正式开始教程:

1.安装前请最好保证以下需求是满足的:

Package Version Notes
GNU Make 3.79, 3.79.1 Makefile/build processor
GCC >=4.7.0 C/C++ compiler1
python >=2.5 Automated test suite2
GNU M4 1.4 Macro processor for configuration3
GNU Autoconf 2.60 Configuration script builder3
GNU Automake 1.9.6 aclocal macro generator3
libtool 1.5.22 Shared library manager3
zlib >=1.2.3.4 Compression library4

gcc-4.8的安装可以参加llvm官网的教程,也可以参见 http://eli.thegreenplace.net/2014/01/16/building-gcc-4-8-from-source-on-ubunu-12-04/

zlib教程一搜就出来了http://myswirl.blog.163.com/blog/static/513186422007101410382259/

其它的基本都满足(ubuntu 12.04.4)(cat /etc/issue)

可以先  apt-get install build-essential  一下

查看各软件版本一般可以用  软件名-v

2.正式开始安装

要安装以下东西

llvm-3.3.src

cfe-3.3.src

clang-tools-extra-3.3.src

compiler-rt-3.3.src

libcxx-3.3.src

教程在这里:  

http://www.cnblogs.com/codemood/p/3142848.html

tips: 不过如果前面环境配置有些问题导致

../llvm-3.3.src/configure --enable-optimized --enable-targets=host-only  不能找到c preprocessor之类的问题,可以自己指定      
CC=你的gcc-4.8的路径(在安装目录下的/bin/gcc) CXX=你的g++-4.8的路径 CPP=同理 ../llvm-3.3.src/configure --enable-optimized --enable-targets=host-only      

3.尝试clang-tutorial

先看Readme 改动 makefile 和 compile_commands.json 

可以先不把Makefile里的sources弄少一点,编译报错后

把函数内容改成现在使用的(根据错误信息改,我记得要把sourceManager改为fileManager)

如果遇到

cannot find -lxxx  请移步这里 http://blog.sciencenet.cn/blog-676535-541444.html

以下是tutorial2的执行情况:

能够得到test.c的各个token

int 'int'
identifier 'footype'
semi ';'
int 'int'
identifier 'main'
l_paren '('
r_paren ')'
l_brace '{'
typedef 'typedef'
unsigned 'unsigned'
long 'long'
identifier 'bartype'
semi ';'
identifier 'printf'
l_paren '('
string_literal '"Hello World\n"'
r_paren ')'
semi ';'
return 'return'
numeric_constant '0'
semi ';'
r_brace '}'
eof ''
           

这是test.c的内容

//#include <stdio.h>
// This code is licensed under the New BSD license.
// See LICENSE.txt for more details.


int i = 4;
extern int j;
typedef int footype;


int main()
{
	typedef unsigned long bartype;
	printf("Hello World\n");
	return 0;
}
           

继续阅读