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……

相關文章
相關標籤/搜索