前言

今天一个同学问我能不能帮忙运行一下一个cpp程序, 但是是linux下mingw标准的.
我电脑不太行, 一直没装虚拟机, 但是知道有wsl(Windows Subsystem for Linux)这个东西, 子系统, 并且可以直接win10软件商城下载, 超级方便, 今天正好顺便学学吧.

安装

Win10 应用商店直接搜索Linux, 会出来很多发行版, 我选择第一个”Ubuntu”

点击下载就可以了

启动

第一期启动, 需要”好几分钟”配置时间
黑色框中显示:”Installing, this may take a few minutes…”

等待一段时间后, 会出来几行内容, 让你创建用户, 输入用户名(自己随便输入一个即可), 然后输入密码, 再然后重复输入一下密码确认, 就行了.

初体验

第一步: 进入管理员模式

很多网上博客没提到的是: 要先进入管理员模式, 不然vim无法保存内容的哦!

1
sudo -i

第二步: 更换镜像源

不会吧, 不会吧, 不会有人下载速度 20 B/s叭?
外国网站访问下载超慢, 但我们可以用阿里/网易/清华的镜像源.

实测在我这里, 阿里比清华快好多倍的样子, 请各位自行测试选择

1. 备份原来的源

其实可有可无

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
2. vim打开并编辑
1
sudo vim /etc/apt/sources.list
3. vim使用

进去直接按i, 进入插入模式
然后把所有没注释的行注释, 找个空把第四条内的源复制进去
按esc, 输入” : + w + q”即可保存退出

4. 镜像源

阿里镜像(20.04版本, 如果过时请直接访问阿里云开发者社区[https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.3e221b11cUMLJB]):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

清华镜像(20.04版本, 如果过时请直接访问清华镜像站[https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/]):

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

其他公司镜像自行百度.

5. 刷新下载源
1
sudo apt-get update

第三步: 安装编译环境

比较统一的是: 只需要安装”build-essential”包就全都有了

1
sudo apt-get install build-essential

安装codeblocks:

1
2
3
4
5
sudo apt-get update

sudo apt-get install codeblocks

sudo apt-get install codeblocks-contrib

第四步: 安装图形用户界面

https://sourceforge.net/projects/xming/
要用到外部软件 Xming
安装后打开Xlaunch,选择 “multiple windows”,一直下一步就行了, 默认端口0
命令输入 DISPLAY=:0 codeblocks,即可打开

成功打开! 恭喜完成各项配置, 你可以在win10系统下使用codeblocks编写C/C++代码, 运行于linux下标准了!

第五步: 各种错误怎么办!

我也遇到国很多错误, 先百度, 实在不行就重装!

在CMD中, 注意是CMD, 输入”wslconfig /u Ubuntu”即可卸载
(cmd中输入wsl也可以进入子系统的)