Windows 10 Linux development environment building (WSL)

Keywords: C++ Linux

1, WSL initialization

Search Linux from the Windows reference store and download Ubuntu (optional 18.04 or 20.04). After downloading, click Install from the start menu and you will be asked to initialize your user name and password.

2, Create root user

Subsequent operations require root permission. Ubuntu does not have root user by default and needs to be created manually first

sudo passwd root
 Input password

3, Change alicloud Download Image

This step is very important and can greatly improve the user experience from Alibaba open source mirror station Copy the corresponding download source, and then follow this step: https://blog.csdn.net/Mingzhul/article/details/105302399

Pay attention to backup before modification, and form good operation habits.

4, Install vscode

With the progress of the times, there seem to be several ways to operate this step, which should be OK

  1. You can refer to this to compile and install VSCode: https://blog.csdn.net/reeeeein/article/details/104628415
  2. Or start it directly through remote WSL: https://blog.csdn.net/weixin_43876113/article/details/105261577 , I don't know if it's because I've installed it in the first step. It's really feasible
  3. Download the VSCode Linux installation package from the official website and install it. I think it is more common. The steps are as follows:

In this step, first install VSCode and remote WSL plug-ins on Windows. Now there are C/C++ Extension Pack plug-ins, which can install a large number of plug-ins developed by C + +, including this one. Then from VSCode official website Download the. deb installation package of Ubuntu, for example, put it on disk D, and then copy it to the home directory (operate as root user):

cp /mnt/d/code_1.62.0-1635954068_amd64.deb ./

Note: WSL and Windows share disks through the / mnt/win disk path, and then install:

dpkg -i code_1.62.0-1635954068_amd64.deb

In this way, vscode is installed on wsl and can be operated on other Linux. The universal vscode can be switched to the code directory. For example, in the / home/testcode directory, vscode can be started by running the command (or directly started by using the remote wsl connection in Windows. The difference is that the workspace name is different after startup...):

cd /home/testcode
code .

This will remotely open the current testcode directory in VScode on Windows.

5, C + + development configuration

Finally, there are some build configurations. In the. VSCode directory under the project directory, generally speaking, this directory and required files will be automatically generated by VSCode. You need to understand its meaning and modify it manually:

C + + project configuration c_cpp_properties.json, which will be used when directly running the project:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [  // Header file lookup path
                "${workspaceFolder}/**",
                "../include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

gdb debugging configures launch.json, which will be used when F5 executes debugging:

{
    // 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": "(gdb) Launch",          //The name displayed in the drop-down menu of the startup configuration
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/testServer",   // The path to the program that will be debugged
            "args": [],
            "stopAtEntry": false,                        // When set to true, the program will pause at the program entrance
            "cwd": "${workspaceFolder}/bin",        // Working directory when debugging programs
            "environment": [],
            "externalConsole": false,                   // Whether to display console window during debugging
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build-debug"                        // The task executed before the start of a debugging session, usually a compiler
        }
    ]
}

settings.json. I don't know what use it is. It seems to be related to the library used:

{
    "files.associations": {
        "functional": "cpp",
        "initializer_list": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "list": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "fstream": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "numeric": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "cfenv": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "typeinfo": "cpp",
        "memory": "cpp",
        "forward_list": "cpp",
        "string": "cpp"
    },
    "cmake.configureOnOpen": true
}

tasks.json, build task configuration, and configure some make parameters

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build-release",
            "command": "make",
            "args": [
                "-j4",
                "-C",
                "${workspaceFolder}/build/" 
            ],
            "type": "shell",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "build-debug",
            "command": "make",
            "args": [
                "-j4",
                "-C",
                "${workspaceFolder}/build/"
            ],
            "type": "shell",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "clean",
            "command": "make",
            "args": [
                "-C",
                "${workspaceFolder}/build/",
                "clean"
            ],
            "type": "shell",
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

Posted by Yippee on Tue, 09 Nov 2021 22:48:12 -0800