0
点赞
收藏
分享

微信扫一扫

wsl安装配置vscode(亲测有用)

我阿霆哥 2022-01-06 阅读 79

文章目录


笔者的环境:win10家庭版,已安装wsl ubuntu

具体安装过程

第一步:打开官方文档开始通过适用于 Linux 的 Windows 子系统使用 Visual Studio Code(https://docs.microsoft.com/zh-cn/windows/wsl/tutorials/wsl-vscode)

依次完成下图所示的两项安装:在windows下安装vscode(记得选择添加到path),安装源程开发扩展包。
在这里插入图片描述
完成以上安装以后,笔者是按照这篇博文https://blog.csdn.net/reeeeein/article/details/104628415/来进行操作的。

第一步:打开linux,默认源切换到阿里云。

sudo sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
sudo apt update -y
sudo apt upgrade -y

第二步之一:下载vscode

sudo apt-add-repository -r ppa:ubuntu-desktop/ubuntu-make
sudo apt update

注:这里是笔者对原文的优化,原文为

第二步之二:切换到root用户
然后执行下面代码安装vscode,(这里需要切换到root模式,使用命令su,再输入root密码,然后执行下面代码安装vscode)

sudo apt install ubuntu-make
sudo umake ide visual-studio-code

第三步:配置和运行vscode
执行成功后,需要切换到普通用户,输入命令exit即可完成切换。
然后输入以下命令:

code .

成功后,然后要在wsl里安装g++和gdb:

sudo apt-get install gdb
sudo apt-get install g++

第四步:配置cpp环境
在自己的文件夹下面创建以下四个文件,保存配置信息
在这里插入图片描述

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [],
            "defines": ["_DEBUG","_UNICODE"],
            "compilerPath": "/usr/bin/g++",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}

launch.json


{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
  
      {
          "name": "g++ build and debug active file",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": false,
          "MIMode": "gdb",
          "setupCommands": [
              {
                  "description": "Enable pretty-printing for gdb",
                  "text": "-enable-pretty-printing",
                  "ignoreFailures": true
              }
          ],
          "preLaunchTask": "g++ build active file",
          "miDebuggerPath": "/usr/bin/gdb"
      }
  ]
}

settings.json

{
  "files.associations": {
    "iostream": "cpp"
  }
}

task.json

{
  "tasks": [
      {
          "type": "shell",
          "label": "g++ build active file",
          "command": "/usr/bin/g++",
          "args": [
              "-g",
              "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "options": {
              "cwd": "/usr/bin"
          }
      }
  ],
  "version": "2.0.0"
}

第五步:创建cpp文件编译运行

在这里插入图片描述
具体编译过程:按F5,选择C++(GDB/LLDB)
在这里插入图片描述
笔者是使用终端进行编译运行如下

azheng@lishizheng:/mnt/e/csapp_bilibili$ g++ main.cpp 
azheng@lishizheng:/mnt/e/csapp_bilibili$ ./a.out 
1

参考

[1]https://docs.microsoft.com/zh-cn/windows/wsl/tutorials/wsl-vscode
[2]https://blog.csdn.net/reeeeein/article/details/104628415/
[3]https://stackoverflow.com/questions/60249177/e-the-repository-http-ppa-launchpad-net-certbot-certbot-ubuntu-focal-release
[4]wsl遇到问题The repository ‘http://ppa.launchpad.net/ubuntu-desktop/ubuntu-make/ubuntu focal Release‘解决方法

举报

相关推荐

0 条评论