WSL2安装ContOS7并更新gcc

1 WSL2安装CentOS7

Windows11官方WSL2已经支持Ubuntu、Open SUSE、Debian。但是没有centos,所以centos的安装方式略有不同。

1.1 下载安装包

下载安装包:https://github.com/mishamosher/CentOS-WSL?tab=readme-ov-file
在这里插入图片描述

1.2 安装

找到一个空间大点的盘,解压这个zip,然后双击CentOS7.exe
在这里插入图片描述
等待一会就安装好了,在powershell中执行

wsl --list

就可以看到安装好的子系统了,我自己装了好几个。

在这里插入图片描述
关于wsl的操作可以执行

wsl --help

查看更多参数

1.3 启动CentOS7

在powershell中执行

wsl -d CentOS7

即可启动CentOS7。

2 CentOS7更换国内源

比较常用的有阿里源,清华源等,这是阿里源的地址,其中有更换源的教程。

3 gcc安装,有两种方式

一种使用使用yum,一种是从源码。推荐使用yum,安装速度快。关于yum的介绍

3.1 使用yum安装gcc 9

执行如下命令

yum update -y
yum install -y centos-release-scl centos-release-scl-rh

先不着急安装gcc,因为访问不到mirrorlist.centos.org,会报如下错误。所以先换成国内的地址
在这里插入图片描述

vi /etc/yum.repos.d/CentOS-SCLo-scl.repo
# 用下面地址替换原来的
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/sclo/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

替换之后是这样的:
在这里插入图片描述

vi /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
# 和/etc/yum.repos.d/CentOS-SCLo-scl.repo一样的操作
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

之后,刷新缓存,安装gcc

yum repolist && yum clean all && yum makecache
yum install -y devtoolset-9
echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc
source /etc/bashrc
gcc --version

在这里插入图片描述

3.2 从源码安装gcc

进到centos里之后,首先从这里这里你需要的gcc版本,然后解压。类似:

wget https://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz
tar xf gcc-9.2.0.tar.gz

如果找不到wget就用yum安装

yum install wget

在下载的同时可以装一下开发工具

yum groupinstall "Development tools" -y

编译安装gcc

cd gcc-9.2.0
# Configure the build, --disable-multilib支持64位,--enable-languages=c,c++仅编译C和C++编译器
./configure --disable-multilib --enable-languages=c,c++

# Build and install
make -j8
sudo make install

3.2.1 卸载源码安装的gcc

gcc没有类似make uninstall的命令,只能手动删除。但是文件比较多,可是使用脚本删除。但是我们不知道要删除哪些文件,可以重新将gcc安装到一个指定临时的路径,之后根据临时路径中的文件,删除默认安装路径(/usr/local/)的文件

rm -rf gcc-9.2.0  # 直接重新configure有些路径还是默认的,所以直接删了,重新解压
tar xf gcc-9.2.0.tar.gz
cd gcc-9.2.0
./configure --disable-multilib --enable-languages=c,c++ --prefix=/home/work/gcc-deps/gcc-9.2.0_install
make -j8
sudo make install
cd /home/work/gcc-deps/gcc-9.2.0_install

# [注意]防止误操作,你可以先执行echo,确认没有问题,再执行rm
find . -type f -print0 | while IFS= read -r -d '' file; do basename="$(basename -- "$file")"; find /usr/local/ -type f -name "$basename" -exec echo {} +; done

find . -type f -print0 | while IFS= read -r -d '' file; do basename="$(basename -- "$file")"; find /usr/local/ -type f -name "$basename" -exec rm {} +; done

4 CMake中使用gcc

安装了gcc 9.2.0,但是cmake中找到的还是gcc 4.8.0,有两种方式设置gcc

  1. 命令行参数
cmake -D CMAKE_C_COMPILER=/path/to/gcc/bin/gcc -D CMAKE_CXX_COMPILER=/path/to/gcc/bin/g++ 
  1. 修改CMakeLists.txt
SET(CMAKE_C_COMPILER "/home/hhb/gcc-9.2.0/bin/gcc")
SET(CMAKE_CXX_COMPILER "/home/hhb/gcc-9.2.0/bin/g++")
project(PROJECT_NAME)  # 注意需要project之前

5 关于linux配置文件

linux下主要有四个配置文件:/etc/profile 、/etc/bashrc 、/root/.bashrc 、/root/.bash_profile。

  • /etc/profile 设置的是系统全局环境和登录系统的一些配置,该配置对所有用户生效; ​
  • /etc/bashrc 是shell全局自定义配置文件,主要用于自定义 shell,该配置对所有用户的shell都生效;
  • /root/.bashrc 用于单独自定义root用户的 bash,只对root用户的bash生效,如果要使elk用户生效,则需要配置/home/elk/.bashrc文件
  • /root/.bash_profile 用于单独自定义root用户的系统环境,只对root用户生效,如果要使elk用户生效,则需要配置/home/elk/.bash_profile。

这四个配置文件的加载顺序如下:

​ /etc/profile -> /etc/bashrc -> /root/.bashrc -> /root/.bash_profile

6 参考

  • https://blog.csdn.net/jiexijihe945/article/details/132067793
  • https://www.cnblogs.com/renshengdezheli/p/14131943.html
  • https://ioflood.com/blog/install-gcc-command-linux/#:~:text=In%20most%20Linux%20distributions%2C%20the,command%20sudo%20yum%20install%20gcc%20.
  • https://stackoverflow.com/questions/19816275/no-acceptable-c-compiler-found-in-path-when-installing-python
  • https://wangchujiang.com/linux-command/c/yum.html#:~:text=yum%E5%91%BD%E4%BB%A4%E6%98%AF%E5%9C%A8Fedora,%E5%9C%B0%E4%B8%80%E6%AC%A1%E6%AC%A1%E4%B8%8B%E8%BD%BD%E3%80%81%E5%AE%89%E8%A3%85%E3%80%82
  • https://stackoverflow.com/questions/17275348/how-to-specify-new-gcc-path-for-cmake
  • https://stackoverflow.com/questions/67090507/how-to-install-gcc-g-9-on-centos-7-docker-centos7
  • https://blog.csdn.net/omaidb/article/details/126462159
  • https://www.codecomeon.com/posts/245/
  • 27
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,wsl2安装CentOS的步骤如下: 1. 首先,在Windows系统上启用Hyper-V功能。 2. 在Hyper-V上安装WSL2(Windows Subsystem for Linux 2)。 3. 在WSL2安装Ubuntu分发版。可通过Microsoft Store或手动下载安装。 4. 在Ubuntu分发版上安装Docker。可以使用以下命令安装Docker: ``` sudo apt update sudo apt install docker.io ``` 5. 安装Docker后,可以使用以下命令来下载并安装CentOS7镜像: ``` sudo docker pull centos:centos7 ``` [2] 这样就完成了在WSL2安装CentOS的过程。请注意,以上步骤仅适用于安装WSL2环境中使用的CentOS镜像。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [windows下wsl2安装centos容器](https://blog.csdn.net/youaremyalllove/article/details/129699210)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [WSL2安装CentOS7(或任何你想安装的Linux发行版)【方法①:利用docker拉镜像,再导出】【方法②:直接利用...](https://blog.csdn.net/u013250861/article/details/129018998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值