Ubuntu使用VS Code开发C++程序

1. 安装VS Codejson

下载地址https://code.visualstudio.com/downloadvisual-studio-code

2. 安装C++扩展程序bash

详情请看https://code.visualstudio.com/docs/languages/cppvisual-studio

https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/spa

打开VS Code后,输入ext install cpptools,等待安装完成。调试

3. 固然打开目录,编辑源码了code

4. 使用g++生成可执行程序/动态链接库regexp

https://code.visualstudio.com/Docs/editor/tasks#_defining-a-problem-matcherorm

建立task.json,下面是gcc命令,改为g++对应的命令便可blog

{
    "version": "0.1.0",
    "command": "gcc",
    "args": ["-Wall", "helloWorld.c", "-o", "helloWorld"],
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

最后按Ctrl+Shift+b

注:修改快捷键设置:

File->Preferences->Keyboard Shortcuts

5. 使用gdb调试

固然第一步是要安装gdb

sudo apt-install gdb

    1) task.json的args里必定要添加-g,完整的task.json相似以下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-o","da.exe","main.c","-g"],
    "showOutput": "always",
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
    }
}

   2) 先生成可执行程序,详见第4点  

   4) 单击左侧的调试按钮,出来的界面上,单击绿色三角(调试)符号,会生成launch.json,根据https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/,默认生成的launch.json包括2个配置:打开新的进程及附加(attach)到已运行的进程,分别是C++ Launch和C++ Attach

     5)尽情的调试吧:D 发现VSCode的快捷键跟Windows下的同样,F十、F11……

相关文章
相关标签/搜索