openss python cmake gcc clang源码编译安装

  1. 在centos7上环境编译bpf程序,依赖clang将c文件编译为bpf格式的.o文件,使用yum安装的clang版本太低,不支持bpf,因此需要源码编译出高版本的clang二进制程序。
  2. 编译高版本的clang又依赖于gcc,使用yum安装的gcc版本为4.8.5,不满足编译clang的需求,因此又需要源码编译安装高版本的gcc。
  3. 编译高版本的gcc,又依赖高版本的cmake和python3,而yum安装的cmake和python又不满足需求,因此又需要编译高版本的cmake和python3。
  4. 编译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

总结

源码安装的生成物包括有二进制文件,动态链接库,头文件。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容