歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 在Linux CentOS上編譯並安裝Clang 3.5.0

在Linux CentOS上編譯並安裝Clang 3.5.0

日期:2017/2/28 14:02:08   编辑:Linux教程

編譯CoreCLR需要Clang 3.5,而CentOS上安裝的是Clang 3.4.2(yum repos中最新版也是這個),只能自己手工編譯LLVM的源代碼進行安裝。

(注:CentOS的版本是6.6)

一、安裝libstdc++4.7

(注:如果是CentOS 7.0,不需要這步)

wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++
ln -s /opt/rh/devtoolset-2/root/usr/bin/* /usr/local/bin/

如果不安裝,在configure配置編譯選項時會出現下面的錯誤提示:

We detected a missing feature in the standard C++ library that was known to be
missing in libstdc++4.6 and implemented in libstdc++4.7. There are numerous
C++11 problems with 4.6's library, and we don't support GCCs or libstdc++ older
than 4.7.

二、安裝Python 2.7.9

(注:如果是CentOS 7.0,不需要這步)

CentOS 6.6自帶的是Python 2.6.6,而編譯llvm需要Python 2.7以上。

1)安裝編譯Python需要的包包

yum install zlib-devel
yum install bzip2-devel
yum install openssl-devel
yum install ncurses-devel
yum install sqlite-devel

2)下載並解壓Python 2.7.9的源代碼

cd /opt
wget --no-check-certificate https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
tar xf Python-2.7.9.tar.xz
cd Python-2.7.9

3)編譯與安裝Python 2.7.9

./configure --prefix=/usr/local
make && make altinstall

4)將python命令指向Python 2.7.9

ln -s /usr/local/bin/python2.7 /usr/local/bin/python

三、添加交換分區

如果編譯的機器內存比較小,建議添加交換分區。我當時測試的機器只有1G內存,在編譯過程中出現了“c++: internal compiler error: Killed (program cc1plus)”錯誤。

1)創建並激活交換文件(比如2G):

dd if=/dev/zero of=/swapfile bs=1k count=2048000
mkswap /swapfile
swapon /swapfile

2)修改 /etc/fstab 文件讓交換文件啟動時自動加載,添加如下內容:

/swapfile               swap                    swap    defaults        0 0

四、編譯LLVM

1)下載llvm的源代碼

http://llvm.org/releases/3.5.0/llvm-3.5.0.src.tar.xz
mv llvm-3.5.0.src llvm

2)下載clang的源代碼

cd llvm/tools
wget http://llvm.org/releases/3.5.0/cfe-3.5.0.src.tar.xz
tar xf cfe-3.5.0.src.tar.xz
mv cfe-3.5.0.src clang

3)下載compiler-rt的源代碼

cd ../projects
wget http://llvm.org/releases/3.5.0/compiler-rt-3.5.0.src.tar.xz
tar xf compiler-rt-3.5.0.src.tar.xz
mv compiler-rt-3.5.0.src compiler-rt

4)配置編譯選項

cd ..
./configure --enable-optimized CC=gcc CXX=g++

5)編譯llvm

make -j2

編譯成功後的提示:

llvm[0]: ***** Completed Release+Asserts Build

6)安裝編譯好的llvm

make install

會安裝在/usr/local/bing中

7)檢查clang的版本

# clang --version
clang version 3.5.0 (tags/RELEASE_350/final)

如果還是舊版本,需要將/usr/bin/clang指向clang 3.5.0:

ls -s /usr/local/bin/clang /usr/bin/clang

更多CentOS相關信息見CentOS 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=14

Copyright © Linux教程網 All Rights Reserved