Using visual studio code in WSL at Windows10

Introduction

VS code is useful.
WSL well.
So a combination of both open from use Shuangwai.
Linux on Windows emulation compiled in several ways

1, MinGW installation, use or gcc compile g ++

2, compiled using Windows Connect WSL

This article describes the second
Note: The items under Linux, on Windows operating compiled, 1,2 two programs are very good.
If want to keep a consistent development environment and production environment, you can use the second to the first possible. g ++ version inconsistencies.

aims

Using the code under Ubuntu vs code editor in Windows, compiled allowed to perform under Ubuntu.

ready

Subsystem on Linux open window

Use Ubuntu, configure Tsinghua source ---> this step is to accelerate the installation of the software. Baidu or do not understand the message.
Process slightly

Vs code installed on Windows

Process slightly,
installation of plug-ins themselves Baidu.
You can change through plug-in software for the Chinese.

operating

According to the official website of way

1, find a directory on a Linux subsystem, then executecode .

This command can open vs code editor for Windows by WSL

Install plug

Ctrl+shift+pOpen the search bar to install plug-in
c / c ++
Remote-WSL
c ++ Intellisence
install the plug when the attention:
需要在WSL中再次安装,双击插件名字旁边的在wsl中安装即可安装.
you need to install the WSL in g ++ gcc ----> solve their own Baidu

2, create a new file hello.cpp

Which is written on C ++ code output hello world

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<string> msg{"hello world", "你好"};

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

Configuration

These three documents are automatically generated by the setting.
In your current directory will generate a directory of .vscode, this directory to store the three files.
These three documents are in control of how the project works.
After you configure this three files to run the code.

c_cpp_properties.json (编译器路径和IntelliSense设置)
tasks.json (任务执行)
launch.json (调试器设置)

tasks.json

tasks.json file to tell VS Code how to build (compile) the program
from the main menu, select Terminal> Configure Default Build Task. In the drop-down list will display task drop-down list, which lists a variety of predefined C ++ compiler build tasks. Select g ++ build active file, it will build the currently displayed (active) file in the editor.
Generate a template file as follows:
Here nothing to modify.
Parameters:
Command Set specifies the program to run; in this case g ++.
args array specifies g ++ will be passed to the command line parameters. These parameters must be specified according to a desired sequence compiler.
This task tells g ++ to obtain active files (${file}), compile, and then in the current directory, (${fileDirname})create a file with the same name but is not active in the extension (${fileBasenameNoExtension})of the executable file.
Label value is what you will see in the task list; your you can be named. This label is in the building when the name appears in the window vscode editor output content you can use Chinese.
Build tasks defined in tasks.json to run, press Ctrl + Shift + Bor select from the main terminal menu "Task: Run build tasks" . Can be carried out.
If you want to run multiple CPP files: You can modify tasks.json to build several C ++ files use "* .cpp" instead of parameters ${file}. You can also replace"${fileDirname}/${fileBasenameNoExtension}"

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

launch.json

Next, you will create a launch.json file to configure VS Code, in order to start the debugger GDB debugger when you press F5.
From the main menu, select "Debug"> "Add Configuration ..." and then select "C ++ (GDB / LLDB) ".
VS Code launch.json create a file, open it in the editor, then build and run the "helloworld".
Parameter Description
program setup to define to be debugged. Here, it is set to the active file folder \ ({fileDirname} and activities filename, without extension \) {} fileBasenameNoExtension, if helloworld.cpp that was active file helloworld.
By default, C ++ extensions will not add any breakpoints in your source code, and its stopAtEntry value is set to false. The stopAtEntry change the value to true will cause the debugger to stop on the main method when you start debugging. (Typically set to false, true if the position of the breakpoint will be generated in the main)

{
  "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"
    }
  ]
}

c_cpp_properties.json

If you want more control over C / C ++ extensions, you can create a c_cpp_properties.json file that will allow you to change the settings, such as the path compiler include path, C ++ standard (default is C ++ 17) and so on.
You can run the command C / C ++: From the command panel (Ctrl + Shift + P) to edit the configuration (UI) to see the C / C ++ configuration UI.
This will open the "C / C ++ Configuration" page. When you make a change here, VS Code will be written to .vscode files in a folder called c_cpp_properties.json in.
仅当程序包含不在工作空间或标准库路径中的头文件时,才需要修改“ 包含路径”设置。
Json contents of the configuration is as follows:

{
  "configurations": [
    {
      "name": "Linux",
      "includePath": ["${workspaceFolder}/**"],
      "defines": [],
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "clang-x64"
    }
  ],
  "version": 4
}

Through the above settings, you can use vscode operation WSL inside the project on Windows.

to sum up

Do seem a little fart pants off.
But you can install vim plugin vscode, using vim in vscode, ah
ha ha ha ha
Note: The following is the official website of reference links Figure words refer to the official website.

Reference
https://code.visualstudio.com/docs/cpp/config-wsl

Guess you like

Origin www.cnblogs.com/dhu121/p/12002590.html
Recommended