VSCode调试Linux下C程序

2019-06-26  本文已影响0人  7644a0b8b5cd

win10下可以自己安装WSL,它是一个Linux系统,安装非常方便,直接在CMD中输入WSL即可进入Linux环境。
利用VSCode可以方便的使用WSL里面的GCC, GDB等C语言编译调试环境,省去了在windows下安装Cygwin这样
的软件的麻烦,还可以体验纯Linux环境下的C语言编程。

下面介绍配置方法:

Windows下程序目录:c:\users\myname\leetcode\CCPlus

WSL下程序目录:\home\myname\leetcode\CCPlus

在Windows的CCPlus目录下的.vscode目录下,保证有如下文件:

c_cpp_properties.json:

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

launch.json

{
    // 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": "/home/feifei/leetcode/CCPlus/main.out",
            "args": ["-fThreading"],
            "stopAtEntry": true,
            "cwd": "/home/feifei/leetcode/CCPlus/",
            "environment": [],
            "externalConsole": true,
            "windows": {
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            "pipeTransport": {
                "pipeCwd": "",
                "pipeProgram": "c:\\windows\\sysnative\\bash.exe",
                "pipeArgs": ["-c"],
                "debuggerPath": "/usr/bin/gdb"
            },
            "sourceFileMap": {
                "/mnt/c": "${env:systemdrive}/",
                "/usr": "C:\\Users\\FF120\\AppData\\Local\\Packages\\KaliLinux.54290C8133FEE_ey8k8hqnwqnmg\\LocalState\\rootfs\\usr\\"
            },
            "preLaunchTask" : "build"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "c:\\windows\\sysnative\\bash.exe",
                "args": ["-c"]
            }
        }
    },
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "/home/feifei/leetcode/CCPlus/main.out",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [],
        }
    ]
}

setting.json

{
    "files.associations": {
        "iostream": "cpp",
        "array": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "optional": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "type_traits": "cpp",
        "tuple": "cpp",
        "typeinfo": "cpp",
        "utility": "cpp",
        "list": "cpp"
    }
}

注意把上面路径相关的替换成你自己的路径,这样就可以编译运行外加调试了。
调试的时候鼠标悬停在变量名称上可直接查看变量的值,和VS2017一样方便。

参考资料

上一篇下一篇

猜你喜欢

热点阅读