在mac上使用vscode創建第一個C++項目

//blog.csdn.net/bujidexinq/article/details/106539523

 

 

準備工作:
安裝好vscode
安裝插件『C/C++』
正式開始:
首先是創建一個空的文件夾(比如文件夾為test),然後在其中新建一個.cpp文件(比如文件為hello.cpp)打開vscode打開test文件夾作為工作目錄,接下來用三步配置好C++開發環境

第一步:
[⇧⌘P]打開命令模式,選擇[C/Cpp: Edit Configurations(JSON)]命令,回車後會自動生成一個.vscode目錄,目錄下有一個c_cpp_properties.json文件,下面給出我的文件示例:

  1.  
    {
  2.  
        “configurations”: [
  3.  
            {
  4.  
                “name”: “Mac”,
  5.  
                “includePath”: [
  6.  
                    “${workspaceFolder}/**”,
  7.  
                    “/Library/Developer/CommandLineTools/usr/include/c++/v1”,
  8.  
                    “/usr/local/include”,
  9.  
                    “/Library/Developer/CommandLineTools/usr/lib/clang/11.0.0/include”,
  10.  
                    “/Library/Developer/CommandLineTools/usr/include”
  11.  
                ],
  12.  
                “defines”: [],
  13.  
                “macFrameworkPath”: [
  14.  
                    “/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks”,
  15.  
                    “/System/Library/Frameworks”,
  16.  
                    “/Library/Frameworks”
  17.  
                ],
  18.  
                “compilerPath”: “/usr/bin/clang”,
  19.  
                “cStandard”: “c11”,
  20.  
                “cppStandard”: “c++17”,
  21.  
                “intelliSenseMode”: “clang-x64”
  22.  
            }
  23.  
        ],
  24.  
        “version”: 4
  25.  
    }

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

  1.  
    {
  2.  
        // See //go.microsoft.com/fwlink/?LinkId=733558
  3.  
        // for the documentation about the tasks.json format
  4.  
        “version”: “2.0.0”,
  5.  
        “tasks”: [
  6.  
            {
  7.  
                “label”: “build c++”,
  8.  
                “type”: “shell”,
  9.  
                “command”: “g++”,
  10.  
                “args”: [
  11.  
                    “${file}”,
  12.  
                    “-std=c++17”,
  13.  
                    “-g”,
  14.  
                    “-Wall”,
  15.  
                    “-lm”,
  16.  
                    “-o”,
  17.  
                    “${fileDirname}/${fileBasenameNoExtension}.out”
  18.  
                ],
  19.  
                “group”: “build”,
  20.  
                “presentation”: {
  21.  
                    “reveal”: “silent”,
  22.  
                    “panel”: “shared”,
  23.  
                    “echo”: true,
  24.  
                    “focus”: false,
  25.  
                    “showReuseMessage”: true,
  26.  
                    “clear”: false
  27.  
                },
  28.  
                “problemMatcher”: “$gcc”
  29.  
            },
  30.  
            {
  31.  
                “label”: “run c++”,
  32.  
                “type”: “shell”,
  33.  
                “dependsOn”: “build c++”,
  34.  
                “command”: “${fileDirname}/${fileBasenameNoExtension}.out”,
  35.  
                “presentation”: {
  36.  
                    “focus”: true
  37.  
                },
  38.  
                “group”: “test”
  39.  
            }
  40.  
        ]
  41.  
    }

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

  1.  
    {
  2.  
        // Use IntelliSense to learn about possible attributes.
  3.  
        // Hover to view descriptions of existing attributes.
  4.  
        // For more information, visit: //go.microsoft.com/fwlink/?linkid=830387
  5.  
        “version”: “0.2.0”,
  6.  
        “configurations”: [
  7.  
            {
  8.  
                “name”: “c/c++ Launch”,
  9.  
                “type”: “cppdbg”,
  10.  
                “request”: “launch”,
  11.  
                “program”: “${fileDirname}/${fileBasenameNoExtension}.out”,
  12.  
                “args”: [],
  13.  
                “stopAtEntry”: false,
  14.  
                “cwd”: “${workspaceFolder}”,
  15.  
                “environment”: [],
  16.  
                “externalConsole”: true,
  17.  
                “MIMode”: “lldb”,
  18.  
                “preLaunchTask”: “build c++”,
  19.  
                “logging”: {
  20.  
                    “trace”: true,
  21.  
                    “traceResponse”: true,
  22.  
                    “engineLogging”: true
  23.  
                }
  24.  
            }
  25.  
        ]
  26.  
    }

完成這三步C++開發環境就配置好了,接下來就可以編譯,運行,調試C++程式了

[⇧⌘B]是編譯程式,[⇧⌘R]是運行程式,如果安裝了插件『Code Runner』可以直接運行程式

如果需要調試,那就按F5,進入調試模式即可

好了,這樣就可以順利地在vscode上進行C++開發啦,趕快行動起來吧~