ubuntu18.04 基於VSCode的C++環境搭建


1 下載

從vscode官網下載最新版本,deb包下載地址:https://code.visualstudio.com/Download

2. 安裝:dpkg -i 安裝包

3 執行code 打開vscode

如果無法啟動,執行

sudo apt-get install libgconf-2-4

//從這一步開始和windows安裝完全一樣。

4 ctrl+shift+x 打開插件欄,安裝c++

4.1 安裝clang和c/c++ Clang Command Adapter

5 創建launch.json

新建文件夾code_c_c++,並打開,提示創建launch.json,點擊創建

然后會彈出來一個launch.json文件,把里面的所有內容全部替換如下,具體內容請查看文末。

6 創建Tasks.json

ctrl+shift+P打開Command Palette,輸入Tasks: Configure Default Build Task → 使用模版創建Tasks.json文件 → Others:

然后會彈出來一個launch.json文件,把里面的所有內容全部替換成下面這些:

7 創建c_cpp_properties.json

ctrl+shift+P打開Command Palette,輸入C/Cpp: Edit configurations...生成c_cpp_properties.json:

內容為:

8 創建setting.json

ctrl+shift+P打開Command Palette,輸入setting,選擇open setting.json

內容替換為

9 新建code文件夾, 編寫helloworld.cpp程序測試,F5調試一下:

10 安裝插件

以下是我覺得還不錯的插件:

11 自定義快捷鍵

1 調出鍵盤快捷鍵方式 Ctrl+k,Ctrl+s
2 搜索對應的command
3 右擊修改快捷方式+enter保存

待補充

12 配置vim

13 配置git

josn文件內容

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", // 配置類型,這里只能為cppdbg
            "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加)
            "program": "${fileDirname}/${fileBasenameNoExtension}.out", // 將要進行調試的程序的路徑
            "args": [], // 程序調試時傳遞給程序的命令行參數,一般設為空即可
            "stopAtEntry": false, // 設為true時程序將暫停在程序入口處,我一般設置為true
            "cwd": "${workspaceFolder}", // 調試程序時的工作目錄
            "environment": [], 
            "externalConsole": false, // 調試時是否顯示控制台窗口,一般設置為true顯示控制台,
                                      // 但是最新版cpptools有BUG,具體請看文末的注意
            "internalConsoleOptions": "neverOpen", // 如果不設為neverOpen,調試時會跳到“調試控制台”選項卡,你應該不需要對gdb手動輸命令吧?
            "MIMode": "gdb", // 指定連接的調試器,可以為gdb或lldb。但目前lldb在windows下沒有預編譯好的版本。
            "miDebuggerPath": "gdb", // 調試器路徑,Windows下后綴不能省略,Linux下則去掉
            "setupCommands": [ // 用處未知,模板如此
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" // 調試會話開始前執行的任務,一般為編譯程序。與tasks.json的label相對應
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile", // 任務名稱,與launch.json的preLaunchTask相對應
            "command": "g++", // 要使用的編譯器, C就寫gcc
            "args": [
                "${file}",
                "-o", // 指定輸出文件名,不加該參數則默認輸出a.exe,Linux下默認a.out
                "${fileDirname}/${fileBasenameNoExtension}.out",
                "-g", // 生成和調試有關的信息
                //"-Wall", // 開啟額外警告
                "-static-libgcc", // 靜態鏈接
                "-std=c11" // C語言最新標准為c11,或根據自己的需要進行修改比如C++17
            ], // 編譯命令參數
            "type": "shell", // 可以為shell或process,前者相當於先打開shell再輸入命令,后者是直接運行命令
            "group": {
                "kind": "build",
                "isDefault": true // 設為false可做到一個tasks.json配置多個編譯指令,需要自己修改本文件,我這里不多提
            },
            "presentation": {
                "echo": true, 
                "reveal": "always", // 在“終端”中顯示編譯信息的策略,可以為always,silent,never。具體參見VSC的文檔
                "focus": true, // 設為true后可以使執行task時焦點聚集在終端
                "panel": "shared" // 不同的文件的編譯信息共享一個終端面板
            },
            //"problemMatcher": "$gcc"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

setting.json

{
    "files.defaultLanguage": "c++", // ctrl+N新建文件后默認的語言
    "editor.formatOnType": true,  // 輸入分號(C/C++的語句結束標識)后自動格式化當前這一行的代碼
    "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳轉點,不用這個就必須手動觸發Intellisense了
    "editor.acceptSuggestionOnEnter": "off", // 我個人的習慣,按回車時一定是真正的換行,只有tab才會接受Intellisense
    // "editor.snippetSuggestions": "top", // (可選)snippets顯示在補全列表頂端,默認是inline

    "code-runner.runInTerminal": true, // 設置成false會在“輸出”中輸出,無法輸入
    "code-runner.executorMap": {
        "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
        "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
        // "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && $dir$fileNameWithoutExt",
        // "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && $dir$fileNameWithoutExt"
    }, // 右鍵run code時運行的命令;未注釋的僅適用於PowerShell(Win10默認),文件名中有空格也可以編譯運行;注釋掉的適用於cmd(win7默認),PS和bash也能用,但文件名中有空格時無法運行
    "code-runner.saveFileBeforeRun": true, // run code前保存
    "code-runner.preserveFocus": true,     // 若為false,run code后光標會聚焦到終端上。如果需要頻繁輸入數據可設為false
    "code-runner.clearPreviousOutput": false, // 每次run code前清空屬於code runner的終端消息,默認false
    "code-runner.ignoreSelection": true,   // 默認為false,效果是鼠標選中一塊代碼后可以單獨執行,但C是編譯型語言,不適合這樣用

    "C_Cpp.clang_format_sortIncludes": true,
    "C_Cpp.errorSquiggles": "Disabled",
    "files.associations": {
        "xstring": "cpp",
        "string": "cpp",
        "algorithm": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstring": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "exception": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iterator": "cpp",
        "limits": "cpp",
        "list": "cpp",
        "memory": "cpp",
        "new": "cpp",
        "queue": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "typeinfo": "cpp",
        "unordered_map": "cpp",
        "utility": "cpp",
        "vector": "cpp",
        "xfacet": "cpp",
        "xhash": "cpp",
        "xlocale": "cpp",
        "xlocinfo": "cpp",
        "xmemory": "cpp",
        "xmemory0": "cpp",
        "xstddef": "cpp",
        "xtr1common": "cpp",
        "xutility": "cpp",
        "*.tcc": "cpp",
        "cmath": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string_view": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "ostream": "cpp",
        "streambuf": "cpp",
        "stack": "cpp"
    }, // 格式化時調整include的順序(按字母排序)
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM