WLS虚拟机的安装与使用

1

打开PowerShell(管理员)

wsl --install

Windows 会安装 WSL 和默认的 Ubuntu。重启电脑。

完成后,重启,查看开始菜单有没有 Ubuntu ,没有就执行下面的。

wsl --list --online

出现类似的:

NAME                            FRIENDLY NAME
Ubuntu                          Ubuntu
Ubuntu-24.04                    Ubuntu 24.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
...

执行安装:

wsl --install -d Ubuntu-24.04

2

在开始菜单搜索Ubuntu,启动终端。
电脑会初始化 Linux,接着创建 Linux 的用户名和密码。用户以后作为home目录下的一个子目录名。
比如用户名设置为 vvv ,那么就会有个新建的目录 /home/vvv,下面的流程都是以 vvv 作为示例

系统可能会询问,选n

Would you like to opt-in to platform metrics collection (Y/n)? To see an example of the data collected, enter 'e'.
[Y/n/e]:

是否愿意向 Canonical 匿名发送一些系统使用统计数据

依次执行

cat /etc/os-release
uname -a
pwd

pwd,是显示当前目录
在终端执行Ubuntu,安装gcc、g++、make、gdb、git

sudo apt update
sudo apt upgrade -y
sudo apt install build-essential gdb git -y

依次执行,看下有没有成功

gcc --version
make --version
gdb --version
git --version

3

打开VS Code,搜索并安装“Extensions”,WSL

回到Ubuntu 终端,执行

code .

随后 Windows 上会自动打开一个新的 VS Code 窗口,注意 VS Code 左下角,会看到WSL: Ubuntu,意味着这个 VS Code 窗口不是普通的 Windows 工作区,而是已经连接到了 Ubuntu

在 Ubuntu 终端建立实验目录

mkdir -p ~/os-study/test
cd ~/os-study/test
code .

~,这个是当前用户的目录,/home/vvv/,在Ubuntu 终端,执行cd ~、pwd,就能看到是位于/home/vvv/

此时,已经可以在目录/home/vvv/os-study/test下开发测试代码了。

4

在目录~/os-study/test下开发测试代码。
新建,c文件

hello.c

写代码

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    printf("Hello from Linux!\n");
    printf("PID = %d\n", getpid());

    return 0;
}

打开 VS Code 自带终端

Terminal → New Terminal

看到:

vvv@DESKTOP-NEH23HN:~/os-study/test$

然后编译

gcc hello.c -o hello

运行:

./hello

5

回到根目录

cd ~

执行:

git clone https://github.com/remzi-arpacidusseau/ostep-code.git

拷贝ostep的代码到当前目录下

然后,进入ostep的代码目录

cd ~/ostep-code
code .

此时的左侧文件树,如下

ostep-code
├── cpu-api
│   ├── p1.c
│   ├── p2.c
│   ├── p3.c
│   └── p4.c
├── cpu-sched-lottery
├── threads-intro
└── ...

打开终端(即Terminal窗口),显示如下:

vvv@DESKTOP-NEH23HN:~/ostep-code$ 

表示现在处于目录/home/vvv/ostep-code
~这个波浪号,就是/home/vvv

在当前目录执行编译

gcc cpu-api/p1.c o cpu-api/p1

以相对目录的方式编译

接着运行

./cpu-api/p1

以相对目录的方式运行

观察终端窗口

posted @ 2026-10-05 06:02  dvdhellohaha  阅读(5)  评论(0)    收藏  举报