Linux CentOS 7.x 上编译并安装 Clang

编译uWebSocetv0.15需要Clang,而CentOS中没有安装clang,只能自己手工编译LLVM的源代码进行安装。我这边是centos7.4,用的是阿里云。

一、安装libstdc++4.7

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/

 二、安装Python 2.7.9

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的源代码,你也可以去官方下载最新的,然后切换到目录:/opt下面

cd /opt
wget -c --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的源代码

wget -c 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 -c 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 -c 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,会安装在/usr/local/bing中

make install

 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

 以上步骤在阿里云和自己使用的电信云主机中成功编译。

[root@master-yzjbz ~]# clang --version
clang version 3.5.0 (tags/RELEASE_350/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix

猜你喜欢

转载自www.cnblogs.com/fit/p/9467401.html