Mac上利用VScode配置c/c++開發環境

Mac上利用VScode配置c/c++開發環境

哭遼,Typora裏面最好不要插入表情,否則保存會閃退c++

  1. 首先你要有一個vscode
  2. 在擴展裏面下載c/c++shell

    第一步

    ⬆+com+p 打開命令模式:選擇c/c++: 編輯配置(edit configuration)
    而後再自動生成的.vscode目錄,打開c_cpp_properties.json。利用老哥的文件示例:json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",                              
                "/Library/Developer/CommandLineTools/usr/include/c++/v1",
                "/usr/local/include",
                "/Library/Developer/CommandLineTools/usr/lib/clang/11.0.0/include",
                "/Library/Developer/CommandLineTools/usr/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks",
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

將要用到的庫添加到includePath裏面工具

第二步:

[⇧⌘P]打開命令模式,選擇[Tasks: Configure Task]命令,選擇的模板爲MSBuild,回車後會自動在.vscode目錄下生成一個tasks.json文件,下面給出個人文件示例:ui

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build c++",
            "type": "shell",
            "command": "g++",
            "args": [
                "${file}",
                "-std=c++17",
                "-g",
                "-Wall",
                "-lm",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "group": "build",
            "presentation": {
                "reveal": "silent",
                "panel": "shared",
                "echo": true,
                "focus": false,
                "showReuseMessage": true,
                "clear": false
            },
            "problemMatcher": "$gcc"
        },
        {
            "label": "run c++",
            "type": "shell",
            "dependsOn": "build c++",
            "command": "${fileDirname}/${fileBasenameNoExtension}.out",
            "presentation": {
                "focus": true
            },
            "group": "test"
        }
    ]
}
  • 不少參數咱也不懂,先照抄吧

該文件其實就是一個命令行構建工具。
把運行程序時在終端輸入的命令和參數對於"command"和"args"的值
輸入shift+command+b,即可構建成功,生成可執行文件「文件名」.outspa

第三步:

[⇧⌘P]打開命令模式,選擇[Debug: Open launch.json]命令,選擇的模板爲C/C++,回車後會自動在.vscode目錄下生成一個launch.json文件,下面給出老哥的文件示例:.net

{
    // 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": "c/c++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "build c++",
            "logging": {
                "trace": true,
                "traceResponse": true,
                "engineLogging": true
            }
        }
    ]
}

完成這三步C++開發環境就配置好了,接下來就能夠編譯,運行,調試C++程序了
[⇧⌘B]是編譯程序,[⇧⌘R]是運行程序,若是安裝了插件『Code Runner』能夠直接運行程序
若是須要調試,那就按F5,進入調試模式便可插件

[⇧⌘B]會編譯生成.out文件,再按[⇧⌘R]會在終端窗口顯示運行結果 或者 進行交互。
參考文章
在mac上使用vscode建立第一個C++項目
Mac在VSCode中搭建C/C++環境命令行

相關文章
相關標籤/搜索