M1 MacBook C + + environment for configuring vs Code (August 2021)

Time:2021-11-23

At the end of the article, I put my three JSON file configurations for reference.

1. Download vs Code

Vs code has been adapted for M1 chip. Go to the official website to download vs Code Apple silicon and install it.

2. Ensure that the clang is installed

Input in the terminalclang --versionCheck whether it is installed. If not, enterxcode-select --installAfter reading the terms, enter agree to install.

3. Download extension

There are three extensions to download.
1.C/C++
2.C++ Intellisense
3.CodeLLDB
4. Chinese (Chinese plug-in, optional)

4. Create a new cpp file

It is directly used hereMicrosoft official documentsCode in.

#include 
#include 
#include 

using namespace std;

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

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

At this time, enter on line 10 of the filemsg.Should be able to see vs code jump outassignTips like that.

5. Configuration file

1. Configure the tasks.json file

First clickterminalConfigure default build tasks C/C++ clang++, a tasks.json file will be generated, which is generated by default and needs to be modifiedargsOption to add a"-std=c++17", modified as

"args": [
				"-g",
				"${file}",
				"-std=c++17",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],

After configuration, you will find the vs Code prompt syntax error. Don’t worry, and then correct it.

2. Configuration C_ cpp_ Properties.json file

Using key combinationsshift+command+p(under Mac, shift isfnKey (the one above), call upC / C + +: edit configuration (JSON), anc_cpp_properties.jsonFile.

take"compilerPath": "/usr/bin/clang",Change to"compilerPath": "/usr/bin/clang++",
take""cppStandard": "c++98",Change to"cppStandard": "c++17",

In fact, the compiler is only modified to clang + +, and the CPP standard is modified to C + + 17

3. Compile the generated file

After this configuration, the vs code will still report two syntax errors, but it doesn’t matter because it hasn’t been updated.
clickterminalRun build task, a binary file will be generated after running, and there are no syntax errors, indicating that we have compiled successfully.
If you want to test, create a new terminal and use. / your binary file name, you can see the output.

4. Configure the launch.json file

clickfunctionAdd configurationC++(GDB/LLDB)clang++, alaunch.jsonFile.
take"type": "cppdbg",Change to"type": "lldb",
At this point, all files are configured.

5. Commissioning

Place a breakpoint on line 10 of the file and clickfunctionStart debugging, you can see the various variables.

My profile

Here are my three JSON file configuration codes.
tasks.json:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"Label": "C / C + +: clang + + generates activity files",
			"command": "/usr/bin/clang++",
			"args": [
				"-g",
				"${file}",
				"-std=c++17",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"Detail": "compiler: / usr / bin / clang + +"
		}
	]
}

c_cpp_properties.json:

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

launch.json:

{
    //Use IntelliSense to understand related properties. 
    //Hover to view the description of an existing property.
    //For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "Name": "clang + + - generating and debugging activity files",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "Prelaunchtask": "C / C + +: clang + + generate activity files"
        }
    ]
}