macOS使用VSCode編輯Cpp(c++)

1.安裝Command Line Tools

1.如果你本身是macOS或者iOS開發(fā)者,一般已經(jīng)在AppStore下載了Xcode,這時候不需要再安裝
2.如果沒有安裝Xcode,也可以去AppStore下載安裝Xcode,但是Xcode至少有10幾G的大小,所以如果為了少占空間,可以去開發(fā)者網(wǎng)站Command Line Tools單獨下載
安裝完成,打開終端:g++ --version

xxx@192 ~ % g++ --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
xxx@192 ~ % 

顯示Apple clang version 14.0.3 (clang-1403.0.22.14.1)代表沒問題,14.0.3版本可能有所差異

2.安裝VSCode

VSCode,如果下載速度太慢,也可以去搜一些網(wǎng)盤等下載。

3.打開VSXCode安裝必要的擴展

  • c/c++


    vscode c cpp.png
  • code runner


    vscode code runner.png

4.創(chuàng)建c++文件,并編譯運行

vscode cpp.png
  • 1.進入工程目錄,沒有的話可以先創(chuàng)建個文件夾打開。
  • 2.創(chuàng)建hello.cppc++文件。
  • 3.編寫一段簡單的cpp代碼。
  • 4.終端使用g++執(zhí)行g++ hello.cpp -o hello.out -W -Wall -g,將hello.cppc++文件編譯成可執(zhí)行文件hello.out。
  • 5.編譯成功會在本目錄下生成hello.out可執(zhí)行文件以及dSYM等調試文件。
  • 6.執(zhí)行./hello.out,運行可執(zhí)行文件
  • 7.輸出結果Hello cpp!%,運行成功。

5.更方便的調試,代碼自動化

vscode setting json set for cpp.png

上面下載的code runner,通過配置可以更方便的管理和調試c++項目

  • 1.更改setting設置會自動生成.vscode目錄和setting.json文件
  • 2.如果不知道哪里修改,可以自己新建.vscode目錄和setting.json文件
  • 3.配置setting.json文件
    • 隱藏dSYM和.out文件
    "files.exclude": {
        // dSYM文件具有調試信息,普通使用的話不看到它就可以了
        "**/*.dSYM": true,
        "**/*.out": true,
    },
  • 自動運行cpp
"code-runner.executorMap": {
    "cpp": "g++ $fullFileName -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c++17 && $dir\"$fileNameWithoutExt\"\".out\"",
  },

完整的setting.json 文件,包含其他配置,配置完成以后,就不再需要第4步的運行了,直接點擊編譯器的三角運行按鈕,或者使用Ctrl Opt M快捷鍵,或者在文件編輯右鍵使用Run Code選項,即可直接查看運行結果。
說明配置中-std=c++17,表示使用c++的17標準版本,可以不添加,也可以根據(jù)自己的需要設置版本。

{
 //font size
    "editor.fontSize": 16,
    // 添加希望被忽略的文件,這樣一些文件雖然存在于當前工作目錄下,但是不會被顯示在左側的文件瀏覽器里
    "files.exclude": {
        // dSYM文件具有調試信息,普通使用的話不看到它就可以了
        "**/*.dSYM": true,
        "**/*.out": true,
    },
    // --------------------------------------------------------------------------------------
  // Code Runner
  // To run code:
  //   use shortcut "Ctrl Opt N" *
  //   or press F1 and then select/type Run Code,
  //   or right click the Text Editor and then click Run Code in editor context menu
  //   or click Run Code button in editor title menu
  //   or click Run Code button in context menu of file explorer
  // To stop the running code:
  //   use shortcut "Ctrl Opt M" *
  //   or press F1 and then select/type Stop Code Run
  //   or right click the Output Channel and then click Stop Code Run in context menu
  "code-runner.executorMap": {
    // Introduction:
    //   Make sure the executor PATH of each language is set in the environment variable.
    //   You could also add entry into "code-runner.executorMap" to set the executor PATH.
    // Supported customized parameters:
    //   $workspaceRoot: The path of the folder opened in VS Code
    //   $dir: The directory of the code file being run
    //   $fullFileName: The full name of the code file being run
    //   $fileName: The base name of the code file being run, that is the file without the directory
    //   $fileNameWithoutExt: The base name of the code file being run without its extension
    /* ------ 編譯、運行只有一個文件的cpp文件 ------ */
    // 注:路徑中有空格不會出現(xiàn)問題
    "cpp": "g++ $fullFileName -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c++17 && $dir\"$fileNameWithoutExt\"\".out\"",
    // 其中 $fullFileName 是絕對路徑,是主文件
    // 自己決定是否加入 && rm $dir\"$fileNameWithoutExt\"\".out\"(也可以添加"files.exclude")
    /* ------ 編譯、運行多個cpp文件 ------ */
    // "cpp": "g++ $fullFileName <file_to_link> -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c++17 && $dir\"$fileNameWithoutExt\"\".out\"",
    // <file_to_link>的寫法:
    //   一般的,你也可以直接寫絕對路徑
    //     \"/path/xxxx.cpp\"
    //   如果你鏈接的cpp文件和主文件在一個目錄下:
    //     $dir\"xxxx.cpp\"
    //   更一般的,如果你鏈接的cpp文件不和主文件在一個目錄下,需要從當前VSCode的工作目錄補充相對路徑從而形成絕對路徑:
    //     $workspaceRoot\"relative/path/xxxx.cpp\"
    /* ------ 編譯c文件 ------ */
    "c": "gcc $fullFileName -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c17 && $dir\"$fileNameWithoutExt\"\".out\"",
    // "c": "gcc $fullFileName <file_to_link> -o $dir\"$fileNameWithoutExt\"\".out\" -W -Wall -O2 -std=c17 && $dir\"$fileNameWithoutExt\"\".out\"",
  },
  // Whether to clear previous output before each run (default is false):
  "code-runner.clearPreviousOutput": true,
  // Whether to save all files before running (default is false):
  "code-runner.saveAllFilesBeforeRun": false,
  // Whether to save the current file before running (default is false):
  "code-runner.saveFileBeforeRun": true,
  // Whether to show extra execution message like [Running] ... and [Done] ... (default is true):
  "code-runner.showExecutionMessage": true, // cannot see that message is you set "code-runner.runInTerminal" to true
  // Whether to run code in Integrated Terminal (only support to run whole file in Integrated Terminal, neither untitled file nor code snippet) (default is false):
  "code-runner.runInTerminal": true, // cannot input data when setting to false
  // Whether to preserve focus on code editor after code run is triggered (default is true, the code editor will keep focus; when it is false, Terminal or Output Channel will take focus):
  "code-runner.preserveFocus": false,
  // Whether to ignore selection to always run entire file. (Default is false)
  "code-runner.ignoreSelection": true,
  // --------------------------------------------------------------------------------------

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容