- 在centos7上环境编译bpf程序,依赖clang将c文件编译为bpf格式的.o文件,使用yum安装的clang版本太低,不支持bpf,因此需要源码编译出高版本的clang二进制程序。
- 编译高版本的clang又依赖于gcc,使用yum安装的gcc版本为4.8.5,不满足编译clang的需求,因此又需要源码编译安装高版本的gcc。
- 编译高版本的gcc,又依赖高版本的cmake和python3,而yum安装的cmake和python又不满足需求,因此又需要编译高版本的cmake和python3。
- 编译python3,又依赖高版本的openssl,yum安装的openssl也不满足需求,又需要源码编译安装高版本的openssl。
以上这些操作,在ubuntu上完全不需要,因为ubuntu上的二进制程序版本一般都比较高,直接使用apt install即可。一方面是有实际需求,另一方也需要尝试一下这些常用软件的源码编译安装,因此便有了此文。
源码编译安装比较耗费磁盘空间,建议虚机配置至少为4C4G50G。
openssl
// centos上默认的openssl版本为1.0.2,编译python3需要1.1.1以上的版本。
[root@localhost Python-3.10.1]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
// 获取源码包
wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz
// 在源码目录下执行以下操作
./config --prefix=/usr/local/openssl #如果此步骤报错,需要安装perl以及gcc包
make && make install
mv /usr/bin/openssl /usr/bin/openssl.bak
ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
// 将高版本的openssl库的头文件链接到标准路径
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
// 将高版本的openssl的动态链接库加入到系统动态库配置中
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v #查看所有的动态链接库
安装完成后,重启虚机再执行openssl version查看openssl版本
python3
// rpm命令查询python包的内容
[root@localhost src]# rpm -ql python
/usr/bin/pydoc
/usr/bin/python
/usr/bin/python2
/usr/bin/python2.7
/usr/libexec/platform-python
/usr/share/doc/python-2.7.5
/usr/share/doc/python-2.7.5/LICENSE
/usr/share/doc/python-2.7.5/README
/usr/share/man/man1/python.1.gz
/usr/share/man/man1/python2.1.gz
/usr/share/man/man1/python2.7.1.gz
// 查询python包的依赖关系
[root@localhost src]# yum deplist python
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.cn99.com
package: python.x86_64 2.7.5-90.el7
dependency: libc.so.6(GLIBC_2.2.5)(64bit)
provider: glibc.x86_64 2.17-325.el7_9
dependency: libdl.so.2()(64bit)
provider: glibc.x86_64 2.17-325.el7_9
dependency: libm.so.6()(64bit)
provider: glibc.x86_64 2.17-325.el7_9
dependency: libpthread.so.0()(64bit)
provider: glibc.x86_64 2.17-325.el7_9
dependency: libpython2.7.so.1.0()(64bit)
provider: python-libs.x86_64 2.7.5-90.el7
dependency: libutil.so.1()(64bit)
provider: glibc.x86_64 2.17-325.el7_9
dependency: python-libs(x86-64) = 2.7.5-90.el7
provider: python-libs.x86_64 2.7.5-90.el7
dependency: rtld(GNU_HASH)
provider: glibc.x86_64 2.17-325.el7_9
provider: glibc.i686 2.17-325.el7_9
源码编译安装python3
// 下载源码
wget https://www.python.org/ftp/python/3.10.1/Python-3.10.1.tar.xz
// 准备目录
mkdir /usr/local/python3
// linux源码安装三板斧,configure,make,make install
./configure --with-ssl --prefix=/usr/local/python3
make -j 4
make install
// 将python3设置为默认的python版本
mv /usr/bin/python /usr/bin/python_old2
ln -s /usr/local/python3/bin/python3 /usr/bin/python
安装完,重启一下虚机,看看系统是否有影响。
重启后,yum不可用,将yum可执行文件的/urs/bin/python改为/usr/bin/python2.7即可。
python 可以都版本共存,遇到需要python2版本的代码,将代码中的python解析器配置为python2的路径即可
cmake
cmake本身并不参与编译,而是生成编译需要的规则文件,如Makefile。
之前老版本的cmake可以直接使用yum remove卸载掉,保留最新的cmake二进制文件即可。
Version 3.13.4 is the minimum required
http://www.cmake.org/cmake/resources/software.html
https://github.com/Kitware/CMake/releases/download/v3.20.6/cmake-3.20.6.tar.gz
// 进入cmake源码目录,执行以下命令
./bootstrap && make -j 4 && sudo make install
支持的Generator分类,可通过命令行指定
Generators
The following generators are available on this platform:
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files (experimental).
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles
= Generates Eclipse CDT 4.0 project files.
KDevelop3 = Generates KDevelop 3 project files.
KDevelop3 - Unix Makefiles = Generates KDevelop 3 project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
cmake中的一些变量设置
Variables are stored in the CMake cache. This is a file named CMakeCache.txt stored at the root of your build directory that is generated by cmake. Editing it yourself is not recommended
可以选release,加快编译速度,缩小生成物的体积。
Sets the build type for make-based generators. Possible values are Release, Debug, RelWithDebInfo and MinSizeRel
CMAKE_BUILD_TYPE:STRING
gcc
https://gcc.gnu.org/wiki/InstallingGCC
https://gcc.gnu.org/install/index.html
https://gcc.gnu.org/install/prerequisites.html
选择一个镜像站下载gcc源码https://gcc.gnu.org/mirrors.html,这里选择
http://mirror.linux-ia64.org/gnu/gcc/releases/gcc-9.4.0/gcc-9.4.0.tar.gz。
// 参考命令
gcc_version=9.4.0
cd gcc-${gcc_version}
// 下载gcc依赖的依赖软件
./contrib/download_prerequisites
// 不能在gcc的源码目录创建obj目录,可以选择与gcc平级的目录
cd ..
mkdir gcc-${gcc_version}-build
cd gcc-${gcc_version}-build
// 这里要配置上disable-multilib,不编译32位
/root/gcc-${gcc_version}/configure --disable-checking --disable-multilib --prefix=/root/gcc-4.0 --enable-languages=c,c++
% make -j$(nproc)
% make install
老版本的gcc可以保留,当需要用到新版本gcc时,指定路径即可
clang,llvm
https://llvm.org/docs/CMake.html
https://llvm.org/docs/GettingStarted.html
https://llvm.org/docs/GettingStarted.html#requirements
github下载源码 https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-10.0.1.tar.gz 这个项目包含了所有llvm相关的组件。
LLVM_ENABLE_PROJECTS:STRING
Control which projects are enabled. For example you may want to work on clang or lldb by specifying -DLLVM_ENABLE_PROJECTS="clang;lldb".
使用新版本gcc,源码安装的gcc未在可搜索路径内,需要特殊指定。
需要指定 -DLLVM_ENABLE_RTTI=ON ,编译bpftrace需要用到这个,待考证。
编译clang+llvm不需要clang,所以可以使用yum remove卸载掉老版本的clang和llvm相关的rpm包
[root@localhost llvm-project-llvmorg-10.0.1]# rpm -qa | grep clang
clang-3.4.2-9.el7.x86_64
[root@localhost llvm-project-llvmorg-10.0.1]# rpm -qa | grep llvm
llvm-3.4.2-9.el7.x86_64
llvm-libs-3.4.2-9.el7.x86_64
[root@localhost llvm-project-llvmorg-10.0.1]# rpm -qa | grep clang
clang-3.4.2-9.el7.x86_64
[root@localhost llvm-project-llvmorg-10.0.1]#
源码编译安装
// 在llvm-project目录创建build目录即可,因为build目录与子项目代码是平级的。
mkdir build
cd build
// 通过环境变量指定gcc工具链的路径
CC=/root/gcc-9.4.0/bin/gcc CXX=/root/gcc-9.4.0/bin/g++ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,/root/gcc-9.4.0/lib64 -L/root/gcc-9.4.0/lib64" -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -G "Unix Makefiles" ../llvm
make -j 4 && make install -j 4
编译安装完成后,执行clang 查看clang的版本
[root@localhost build]# ./bin/clang -v
clang version 10.0.1
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /root/llvm-project-llvmorg-10.0.1/build/./bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
总结
源码安装的生成物包括有二进制文件,动态链接库,头文件。