To successfully complete this tutorial, you must do the following steps:
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.
mkdir projects
cd projects
mkdir helloworld
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!
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.
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.
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
:
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后,就可以配置远程开发环境了,即代码存储在远程服务器上,调试和运行也在远程服务器,省去了在本地配置的麻烦
Tip: PuTTY for Windows is not a supported client, but you can convert your PuTTYGen keys.
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.
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上,如图: