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.
相关推荐
我星期八休息5 小时前
IT疑难杂症诊疗室:AI时代工程师Superpowers进化论
linux·开发语言·数据结构·人工智能·python·散列表
热心网友俣先生5 小时前
2026年第二十三届五一数学建模竞赛C题超详细解题思路+各问题可用模型推荐+部分模型结果展示
c语言·开发语言·数学建模
01漫游者5 小时前
JavaScript函数与对象增强知识
开发语言·javascript·ecmascript
IGAn CTOU5 小时前
Java高级开发进阶教程之系列
java·开发语言
csbysj20205 小时前
SQL NULL 函数详解
开发语言
其实防守也摸鱼5 小时前
CTF密码学综合教学指南--第三章
开发语言·网络·python·安全·网络安全·密码学
NGSI vimp5 小时前
Java进阶——如何查看Java字节码
java·开发语言
A7bert7775 小时前
【YOLOv8pose部署至RDK X5】模型训练→转换bin→Sunrise 5部署
c++·python·深度学习·yolo·目标检测
li1670902706 小时前
第二十七章:智能指针
c语言·数据结构·c++·visual studio
We་ct6 小时前
深度剖析浏览器跨域问题
开发语言·前端·浏览器·跨域·cors·同源·浏览器跨域