系统相关
首页 > 系统相关> > Windows10 Linux开发环境搭建(WSL)

Windows10 Linux开发环境搭建(WSL)

作者:互联网

一、WSL初始化

从Windows引用商店搜搜Linux,下载Ubuntu(可选18.04或者20.04都可),下载完成后开始菜单点击安装,会要求初始化用户名和密码。

二、创建root用户

后续操作需要root权限,Ubuntu默认是没有root用户的,需要先手动创建

sudo passwd root
输入密码

三、更改阿里云下载镜像

这一步非常重要,可以极大提升用户体验,从阿里开源镜像站复制对应下载源,然后按这个步骤操作:https://blog.csdn.net/Mingzhul/article/details/105302399

注意先备份再修改,养成良好操作习惯。

四、安装vscode

随着时代的进步,这一步的操作貌似有几种方式,应该都可以吧

  1. 可以参考这个,编译安装VSCode:https://blog.csdn.net/reeeeein/article/details/104628415
  2. 或者直接通过 Remote-WSL 直接启动:https://blog.csdn.net/weixin_43876113/article/details/105261577 ,不知道是不是我已经用第一步安装了的原因,这样的确是可行的
  3. 从官网下载VSCode Linux安装包安装,我觉得这种比较通用,步骤如下:

这一步需要首先在Windows上安装好VSCode及 Remote-WSL插件,现在有 C/C++ Extension Pack 插件,可以装一大堆C++开发的插件,其中就有这个。 然后从VSCode官网下载Ubuntu的.deb安装包,比如放到D盘,然后拷贝到home目录下(以root用户操作):

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

注意:WSL和Windows通过 /mnt/win磁盘路径 共享磁盘,然后安装:

dpkg -i code_1.62.0-1635954068_amd64.deb

这样就再wsl上安装好了VScode,在其他Linux上都可以这么操作,万能的VSCode,然后切换到代码目录,例如在 /home/testcode 目录下,VScode可以运行命令启动(也可直接在Windows中用Remote-WSL连接启动,区别在于启动后工作空间名字不一样...):

cd /home/testcode
code .

这样就会在Windows上的VScode中远程打开当前 testcode 目录。

五、C++开发配置

最后就是一些构建配置,在工程目录下的 .vscode 目录下,一般来说这个目录和需要的文件会由VSCode自动生成,需要了解其含义,适当手动修改:

C++工程配置 c_cpp_properties.json,会在直接运行项目时用到:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [  // 头文件查找路径
                "${workspaceFolder}/**",
                "../include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

gdb调试配置 launch.json ,会在F5执行调试时用到:

{
    // 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",          //启动配置的下拉菜单中显示的名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/testServer",   // 将要进行调试的程序的路径
            "args": [],
            "stopAtEntry": false,                        // 设为true时程序将暂停在程序入口处
            "cwd": "${workspaceFolder}/bin",        // 调试程序时的工作目录
            "environment": [],
            "externalConsole": false,                   // 调试时是否显示控制台窗口
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build-debug"                        // 调试会话开始前执行的任务,一般为编译程序
        }
    ]
}

settings.json,这个不知道有什么用,貌似与用到的库有关:

{
    "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 ,构建任务配置,配置了一些 make 参数

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

标签:VSCode,WSL,Windows10,workspaceFolder,Windows,build,Linux,cpp
来源: https://www.cnblogs.com/jixiaohua/p/15533539.html