To prepare

To install clang, you need to install it first. There are two ways to install clang on a Mac:

  1. Install it on the AppStoreXcode.clangWill be inXcodeAutomatic installation during installation
  2. Execute on the command line terminalxcode-select --installTo install

After the installation is complete, run the clang -v command on the CLI to verify the installation

$ clang -v
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Copy the code

Install VS Code

  • Download VS Code from the VS Code website
  • Double-click to install

VS Code plug-in installed

To run C++ programs, you need to install themC/C++Plug-in, openVS CodeTo enter the plug-in extension (shortcut keysCommand + Shift + X), the searchC/C++Plug-in installationIf you don’t understand English, you can install a Chinese plug-inChinese (Simplified) Language Pack for Visual Studio Code , installation method and installationC/C++As a plugin, replace the search term withChineseIt is ok

After the plug-in is installed, restart VS Code for it to take effect

Build and run the first C++ program

Use the following steps to create a project directory, firstProject, and open it using VS Code

$ mkdir firstProject
$ cd firstProject
$ code .
Copy the code

createmain.cppThe file code is as follows

#include <iostream>
using namespace std;

int main(a)
{
    cout << "hello world!" << endl;
    return 0;
}
Copy the code

According to theCommand + Shift + POpen the command line panel and entertasks, the choice ofTasks:Configure Taskgeneratetasks.jsonThe configuration fileChoose hereC/C++:clang build active fileTemplate, it doesn’t matter if you choose another template, you can overlay it underneath VS CodeAutomatically in.vscodeDirectory generationtasks.jsonThe configuration fileChange the file content to

{
    "version": "2.0.0"."tasks": [{"label": "build"."command": "clang++"."args": [
                "-o"."main".// Execute file name
                "main.cpp".// The source file to execute
                "-g"."-v"]."type": "shell"."presentation": {
                "echo": true."reveal": "always"."panel": "shared"
            },
            "problemMatcher": {
                "owner": "cpp"."fileLocation": [
                    "relative"."${workspaceRoot}"]."pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$"."file": 1."line": 2."column": 3."severity": 4."message": 5}}}]}Copy the code

Then press Command + Shift + B to compile the source file. After compiling, the main file will be generated in the project path./main can be used to execute the file

$ ./main
hello world!
Copy the code

Project commissioning

If you need to debug your project, create a launch.json file and configure it accordingly

  • According to theCommand + Shift + DThe running page is displayed

  • Click on theCreate the launch.json file, the choice ofC++(GDB/LLDB)

Again, it’s going to be in.vscodeCreate a folderlaunch.jsonfile

Modify thelaunch.jsonThe content of the file is

{
    "version": "2.0.0"."configurations": [{"name": "C++ Launch"."type": "cppdbg"."request": "launch"."program": "${workspaceRoot}/main".// Main is the same as the execution file name in tasks.json
            "preLaunchTask": "build"."internalConsoleOptions": "openOnSessionStart"."logging": {
                "moduleLoad": false."programOutput": true."trace": false
            },
            "showDisplayString": false."args": []."stopAtEntry": false."cwd": "${workspaceRoot}"."environment": []."externalConsole": false.// set true to enable keyboard input
            "osx": {
                "MIMode": "lldb"}}}]Copy the code

incoutStatement on the breakpoint, and then click the run button to proceeddebug Reference:Build and Debug C++ on Visual Studio Code for Mac

At this point, the construction of the C++ project environment using VS Code is complete

If feel helpful to you can pay attention to my public numberhuangxyAnd share some technical articles from time to time