You can choose Visual Studio Code or Xcode. Xcode is recommended because Xcode must be installed before you can use Visual Studio Code to write C++ Code. Additionally, Visual Studio Community for Mac does not support C++ programming.

The system environment is macOS 10.15 Catalina

Visual Studio Code

Visual Studio Code for short VS Code.

  • VS Code download here. As of March 9, 2020, VS Code is version 1.43.0.

  • Chinese (Simplified) Language Pack for Visual Studio Code can be searched in the VS Code store

  • Install C/C++ extensions in VSCode store.

VS Code’s C/C++ extension does not include a C++ compiler or debugger. You need to install Clang for Xcode on macOS. Xcode is available on the App Store. In the VS Code terminal: g++ –help

  • Add VS Code to the system PATH environment variable. View the following image from the VS Code main menu -> Command panel… In the open:

  • Create a new empty folder, such as Hellworld, and open it in WORKSPACE mode in VS Code:

  • Selecting C/C++: Edit Configuration (UI) in the command panel automatically generates the c_cpp_properties.json file.

The c_cpp_properties.json file looks like this:

{
    "configurations": [{"name": "Mac"."includePath": [
                "${workspaceFolder}/ * *"]."defines": []."macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Framework s"]."compilerPath": "/usr/bin/clang"."cStandard": "c11"."cppStandard": "c++17"."intelliSenseMode": "clang-x64"}]."version"4} :Copy the code
  • Select a task in the command panel: Configure default task generation ->Others The task.json file is automatically generated when you run any external command.

The default task.json file is as follows:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0"."tasks": [{"label": "echo"."type": "shell"."command": "echo Hello"}}]Copy the code

Replace the task.json file with the following:

{
  "version": "2.0.0"."tasks": [{"label": "Build with Clang"."type": "shell"."command": "clang++"."args": [
        "-std=c++17"."-stdlib=libc++"."helloworld.cpp"."-o"."helloworld.out"."--debug"]."group": {
        "kind": "build"."isDefault": true}}}]Copy the code
  • Json ->C++(GDB/LLDB). The file will be generated automatically.

The default generated launch.json file looks like this:

{// Use IntelliSense to learn about related attributes. // Hover to view descriptions of existing properties. / / for more information, please visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0"."configurations": [{"name": "(LLDB) start"."type": "cppdbg"."request": "launch"."program": "Enter the program name, for example${workspaceFolder}/a.out"."args": []."stopAtEntry": false."cwd": "${workspaceFolder}"."environment": []."externalConsole": false."MIMode": "lldb"}}]Copy the code

will

"program": "Enter the program name, for example${workspaceFolder}/a.out".Copy the code

Replace with:

"program": "${workspaceFolder}/helloworld.out".Copy the code

Same as hellowrold. Out in task.json.

  • Add a C++ source file helloworld.cpp with the following example code:
#include <iostream>

int main() {
    std::cout << "Hello cpp";
    return 0;
}
Copy the code

The workspace directory structure is as follows. Note that the helloworld.cpp location is at the same level as the.vscode location:

  • Select from the command panelTask: Run the build taskOr press the shortcut key combination⇧ ⌘ B, or chooseTerminal -> Run build tasks…, compile and run the program:

  • Enable debugger:

Warning: Debuggee TargetArchitecture not detected, Assuming x86_64, you can add the following configuration to launch.json:

"targetArchitecture": "x86_64"
Copy the code

The final result is:

  • The c_cpp_properties.json tasks.json launch.json files can be copied to other workspaces for reuse by replacing the CPP and out files.

  • Reference: code.visualstudio.com/docs/cpp/co…

Xcode

The Xcode version information is as follows:

File->New->Project Select the Command Line Tool Project template type for macOS:

Language is C or C++, select C in the figure below:

C file will be generated by default. Click “Run” in the upper left. The result is as follows:

Select C++ and generate main. CPP file by default. Click “Run” in the upper left.

Q&A

  • *** is not a valid command