WSL2 + vscode的c++开发环境搭建

WSL2 + vscode的c++开发环境搭建

1. 在windows中安装WSL2

如果你是windows10 2004版本或者更新的版本,可以直接使用简单的命令行安装办法,在cmd中运行:

wsl.exe --install

这个命令会自动下载linux内核并自动安装ubuntu发行版。
若ubuntu安装失败并报错

0x800701bc

可以尝试参考这篇文章的解决办法:
更新WSL内核

2.Vscode

在windows系统的Vscode中安装Remote wsl扩展插件后,在wsl的终端中运行:

code .

等到Vscode界面弹出,左下角显示连接WSL成功即可。
在WSL子系统中创建一个test.cpp文件

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
   vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

   for (const string& word : msg)
   {
      cout << word << " ";
   }
   cout << endl;
}

此时直接在terminal中g++编译可能会报错,这时候在VScode在标题栏中选择Terminal > Configure Default Build Task. 在下拉栏中,选择 g++ build active file.会自动创建一个task.json的文件。
回到test.cpp源码处,按下Ctrl + Shift + B,最后可以在终端中看见编译的输出信息。./test即可运行


版权声明:本文为qq_42683901原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。