Centos7部署.net8和升级libstdc++

最近有一个系统需要在centos7.9平台上部署,而且需要用到.net8。

然后就按照网上的文章下载了一个二进制包,设置环境变量,运行dodnet报错,centos7.9的依赖不支持。

wget -P /tgz/ https://builds.dotnet.microsoft.com/dotnet/Sdk/8.0.416/dotnet-sdk-8.0.416-linux-x64.tar.gz

shell 复制代码
[root@testmysqldotnet]# pwd
/app/dotnet
[root@testmysqldotnet]# ls -lh
-rwxr-xr-x. 1 1000 1000  67K 10月 28 05:32 dotnet
drwxrwxr-x. 3 1000 1000   17 10月 28 05:42 host
-rw-rw-r--. 1 1000 1000 1.1K 10月 28 05:15 LICENSE.txt
drwxrwxr-x. 6 1000 1000  150 10月 28 22:51 packs
drwxrwxr-x. 3 1000 1000   21 10月 28 22:51 sdk
drwxrwxr-x. 3 1000 1000   21 10月 28 22:51 sdk-manifests
drwxrwxr-x. 4 1000 1000   67 10月 28 22:51 shared
drwxrwxr-x. 3 1000 1000   20 10月 28 22:51 templates
-rw-rw-r--. 1 1000 1000  93K 10月 28 05:15 ThirdPartyNotices.txt
[root@testmysqldotnet]# ./dotnet -v
./dotnet: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./dotnet)
./dotnet: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./dotnet)

看了很多教程,基本上就这几点解决方法

  1. 升级操作系统,我要能升级就不会来这问了

    yum升级libstdc++,但是就是不告诉你yum源在哪,怎么配置。而且大都是AI生成的LA JI文章。而且升级底层库对系统有一定影响,某些系统服务(如 systemd、dbus)可能依赖旧 ABI。

  2. 使用docker,没有原生二进制部署方便

这里我参考这篇文章 https://zhuanlan.zhihu.com/p/498529973

,编译新版的libstdc++,然后使用 rpath,或更现代的 $ORIGIN + RUNPATH,这样比全局替换 /usr/lib64/libstdc++.so.6 更稳定可靠,影响最小。因为只影响目标程序,不影响系统其他部分

1.下载源码

复制代码
wget http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz

2.编译gcc/libstdc++

复制代码
cd gcc-7.3.0
./contrib/download_prerequisites

3.配置生成Makefile

复制代码
mkdir build;cd build

../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

4.执行编译,编译时间较长,大约1小时,建议使用多线程编译,再加上nohup放到后台执行

复制代码
nohup make -j 8 2>&1 >> make.log &

5.编译日志我这里是12mb,比较大。

复制代码
[root@testmysqlbuild]# pwd
/tgz/gcc-7.3.0/build
[root@testmysqlbuild]# ll -h make.log 
-rw-r--r--. 1 root root 11M 12月  2 13:52 make.log

6.使用find在编译目录找到产物,这个文件libstdc++.so.6.0.24放到/app/libs/libstdc++.so.6.0.24,然后创建软连接。

shel 复制代码
[root@testmysqlbuild]# ll `find /tgz/ -name libstdc++.so*`
lrwxrwxrwx. 1 root root       19 12月  2 13:47 /tgz/gcc-7.3.0/build/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so -> libstdc++.so.6.0.24
lrwxrwxrwx. 1 root root       19 12月  2 13:47 /tgz/gcc-7.3.0/build/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6 -> libstdc++.so.6.0.24
-rwxr-xr-x. 1 root root 11506368 12月  2 13:47 /tgz/gcc-7.3.0/build/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.24
lrwxrwxrwx. 1 root root       19 12月  2 13:40 /tgz/gcc-7.3.0/build/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so -> libstdc++.so.6.0.24
lrwxrwxrwx. 1 root root       19 12月  2 13:40 /tgz/gcc-7.3.0/build/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6 -> libstdc++.so.6.0.24
-rwxr-xr-x. 1 root root 11506368 12月  2 13:40 /tgz/gcc-7.3.0/build/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.24
lrwxrwxrwx. 1 root root       19 12月  2 13:51 /tgz/gcc-7.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so -> libstdc++.so.6.0.24
lrwxrwxrwx. 1 root root       19 12月  2 13:51 /tgz/gcc-7.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6 -> libstdc++.so.6.0.24
-rwxr-xr-x. 1 root root 11506368 12月  2 13:51 /tgz/gcc-7.3.0/build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.24
复制代码
[root@testmysqllibs]# ln -sf libstdc++.so.6.0.24 libstdc++.so.6         ^C
[root@testmysqllibs]# pwd
/app/dotnet-8.0.22/libs
[root@testmysqllibs]# ls
libstdc++.so.6.0.24
[root@testmysqllibs]# ln -sf libstdc++.so.6.0.24 libstdc++.so.6

7.安装修改程序。(使用阿里centos7的yum就够了)

复制代码
sudo yum install -y patchelf || sudo dnf install -y patchelf

# 给 dotnet 二进制文件添加 RUNPATH:指向 $ORIGIN/libs
patchelf --set-rpath '$ORIGIN/libs' dotnet

# 验证是否成功
readelf -d dotnet | grep -E 'RPATH|RUNPATH'
 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/libs]

8.再次测试,成功解决

shell 复制代码
[root@testmysqldotnet-8.0.22]# ./dotnet -h

Usage: dotnet [host-options] [path-to-application]

path-to-application:
  The path to an application .dll file to execute.

host-options:
  --additionalprobingpath <path>  Path containing probing policy and assemblies to probe for.
  --depsfile <path>               Path to <application>.deps.json file.
  --runtimeconfig <path>          Path to <application>.runtimeconfig.json file.
  --fx-version <version>          Version of the installed Shared Framework to use to run the application.
  --roll-forward <value>          Roll forward to framework version (LatestPatch, Minor, LatestMinor, Major, LatestMajor, Disable)
  --additional-deps <path>        Path to additional deps.json file.
  --list-runtimes                 Display the installed runtimes
  --list-sdks                     Display the installed SDKs

Common Options:
  -h|--help                       Displays this help.
  --info                          Display .NET information.
相关推荐
微学AI7 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
豆瓣鸡8 小时前
算法日记 - Day3
java·开发语言·算法
韭菜炒鸡肝天9 小时前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
小王C语言9 小时前
【7. 实现登录注册模块】:实现会话管理、注册/登录/退出 API
网络·c++
Aaron - Wistron9 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
界面开发小八哥10 小时前
界面控件DevExpress Blazor v26.1新版亮点 - 辅助功能增强
.net·界面控件·blazor·devexpress·ui开发
Dxy123931021610 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
05664611 小时前
Python康复训练——常用标准库
开发语言·python·学习
骊城英雄11 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#
吾儿良辰11 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#