Macbook air M1 configuring the vscade C + + Environment

Download and install

from vscode official website download address Download the installation package and select Apple Silicon

After downloading, it seems that it will be installed automatically

Double click to open

If your English level is OK, it is recommended to refer to the official documents directly for the subsequent configuration process config-clang-mac

The following is my configuration process for reference...

Installation related dependencies

C/C + + plug-in


Simplified Chinese

lldb adapter

Apple has customized the clang+llvm compiler and lldb debugger for its own system, which has better performance and can replace gcc and gdb.

Make sure that clang and Xcode are installed in the mac

Create code

  1. Open vscode from the specified path through the code command

    If the code command is invalid, you need to install it:
    command + shift + p

    If an unauthorized error occurs, you need to set the permission of / usr/local/bin / so that the current user can read and write the path.
  2. Create a new file and edit the source code:

    hello_world.cpp
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

There is one more. vscode directory in the current workspace

  1. Set auto save

Configuration.json

Three files need to be configured in vscode:

  1. tasks.json (build instructions)
    Configure compiler instructions to tell vscode how to build (compile) programs.
  2. launch.json (debugger settings)
    Configure debugger directives.
  3. c_cpp_properties.json (compiler path and IntelliSense settings)
    Configure the IntelliSense of the compilation path and editor.

Sometimes, only tasks.json can compile normally.

tasks.json



Edit as follows

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
}

Tell the compiler to use the C++17 standard compiler.
Change the current working directory to the directory where. vscode is located.

Compile the source code and generate an executable file
command + shift + b

When compiling, place the cursor in hello_ In world.cpp

Executive document

launch.json

fn + f5

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "clang++ build active file",
      "targetArchitecture": "arm64"
    }
  ]
}

Debugging error...

But you can use the command line

I'm so bored. I'd better debug with the command line...

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

I feel that the configuration of the compilation path (header file path and library path) should be placed in tasks.json, c_cpp_properties.json only the configuration editor displays the relevant parameters.

If you use MacBook, you can only use vscode as an editor. Compilation can be completed through Makefile, and debugging can be carried out through lldb command line.

Keywords: C++ Visual Studio Code

Added by day_tripperz on Tue, 21 Sep 2021 12:04:39 +0300