Prerequisites

To successfully complete this tutorial, you must do the following steps:

  1. Install Visual Studio Code.

  2. Install the WSL extension.

  3. Install Windows Subsystem for Linux and then use the links on that same page to install your Linux distribution of choice. This tutorial uses Ubuntu. During installation, remember your Linux user password because you’ll need it to install additional software.

配置vscode + wsl 本地开发环境

wsl的安装有多种方式,请先检查windows的wsl是否已启用,否则安装会失败:

在这里插入图片描述

配置wsl环境

  1. 通过开始菜单,找到ubuntu,然后打开,创建用户名和密码,完成初始化
  2. 进入到命令行,然后创建一个工程目录:
mkdir projects
cd projects
mkdir helloworld
  1. Although you will be using VS Code to edit your source code, you’ll be compiling the source code on Linux using the g++ compiler. You’ll also debug on Linux using GDB. These tools are not installed by default on Ubuntu, so you have to install them. Fortunately, that task is quite easy!

  2. From the WSL command prompt, first run apt-get update to update the Ubuntu package lists. An out-of-date distro can sometimes interfere with attempts to install new packages.

sudo apt-get update
5. If you like, you can run sudo apt-get update && sudo apt-get dist-upgrade to also download the latest versions of the system packages, but this can take significantly longer depending on your connection speed.

  1. From the command prompt, install the GNU compiler tools and the GDB debugger by typing:

sudo apt-get install build-essential gdb
Verify that the install succeeded by locating g++ and gdb. If the filenames are not returned from the whereis command, try running the update command again.

whereis g++
whereis gdb

Note: The setup steps for installing the g++ compiler and GDB debugger apply if you are working directly on a Linux machine rather than in WSL. Running VS Code in your helloworld project, as well as the editing, building, and debugging steps are the same.

Run VS Code in WSL

记得安装wsl插件
  1. Navigate to your helloworld project folder and launch VS Code from the WSL terminal with code .:
cd $HOME/projects/helloworld
code .   #看仔细了,这里是code .,一定不要丢了后面的.

You’ll see a message about “Installing VS Code Server”. VS Code is downloading and installing a small server on the Linux side that the desktop VS Code will then talk to. VS Code will then start and open the helloWorld folder. The File Explorer shows that VS Code is now running in the context of WSL with the title bar WSL: Ubuntu.
在这里插入图片描述
You can also tell the remote context from the Status bar.
在这里插入图片描述
If you click on the Remote Status bar item, you will see a dropdown of Remote commands appropriate for the session. For example, if you want to end your session running in WSL, you can select the Close Remote Connection command from the dropdown. Running code . from your WSL command prompt will restart VS Code running in WSL.

The code . command opened VS Code in the current working folder, which becomes your "workspace". As you go through the tutorial, you will see three files created in a .vscode folder in the workspace:

  1. c_cpp_properties.json (compiler path and IntelliSense settings)
  2. tasks.json (build instructions)
  3. launch.json (debugger settings)

Install the C/C++ extension

Once you create the file and VS Code detects it is a C++ language file, you may be prompted to install the Microsoft C/C++ extension if you don’t already have it installed.
在这里插入图片描述
Choose Install and then Reload Required when the button is displayed in the Extensions view to complete installing the C/C++ extension.

If you already have C/C++ language extensions installed locally in VS Code, you’ll need to go to the Extensions view (Ctrl+Shift+X) and install those extensions into WSL. Locally installed extensions can be installed into WSL by selecting the Install in WSL button and then Reload Required.
在这里插入图片描述
到此完成了vscode + wsl的配置,更详细的请见这里

配置vscode + 远程(vscode连远程服务器)开发环境

安装好vscode后,就可以配置远程开发环境了,即代码存储在远程服务器上,调试和运行也在远程服务器,省去了在本地配置的麻烦

打开vscode,安装remote 插件

在这里插入图片描述

配置ssh

  1. Configuring key based authentication
    SSH public key authentication is a convenient, high security authentication method that combines a local “private” key with a “public” key that you associate with your user account on an SSH host. This section will walk you through how to generate these keys and add them to a host.

Tip: PuTTY for Windows is not a supported client, but you can convert your PuTTYGen keys.

  1. Quick start: Using SSH keys
    To set up SSH key based authentication for your remote host. First we’ll create a key pair and then copy the public key to the host.

Create your local SSH key pair

Check to see if you already have an SSH key on your local machine. This is typically located at ~/.ssh/id_ed25519.pub on macOS / Linux, and the .ssh directory in your user profile folder on Windows (for example C:\Users\your-user\.ssh\id_ed25519.pub).

If you do not have a key, run the following command in a local terminal / PowerShell to generate an SSH key pair:

ssh-keygen -t rsa -b 4096

Tip: Don’t have ssh-keygen? Install a supported SSH client.

  1. In VS Code, run Remote-SSH: Open Configuration File… in the Command Palette (F1), select an SSH config file, and add (or modify) a host entry as follows:
Host name-of-ssh-host-here
    User your-user-name-on-host
    HostName host-fqdn-or-ip-goes-here
    IdentityFile ~/.ssh/id_ed25519-remote-ssh

例如:
F1之会就弹出:
在这里插入图片描述
在这里配置就好了,配置如下所示(我直接放在了.ssh/config文件里):
在这里插入图片描述

Tip: You can use / for Windows paths as well. If you use \ you will need to use two slashes. For example, C:\path\to\my\id_ed25519.
完成上面的配置后,按F1就可以选择自己配置的机器,连接了,如果在生成key的时候输入了密码,那么这一步就要输入密码才能连接,如图:
在这里插入图片描述
当然,也需要把"C++ "安装在remote host上,如图:
在这里插入图片描述

最后

  1. 一定记得在linux安装gcc/g++开发环境
  2. 一定记得在linux安装gdb调试工具,有些默认没有安装,即使正确配置了launch.json和task.json,也会报错。。。
  3. 分享一下我的配置:
    在这里插入图片描述
    在这里插入图片描述
GitHub 加速计划 / ws / WSL
17.09 K
797
下载
Issues found on WSL
最近提交(Master分支:1 个月前 )
86fa5afa 2 个月前
e899d0b7 2 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐