Visual studio code debug C + + configuration under Mac and windows

Time:2020-10-31

Mac tutorial

precondition

To successfully complete this tutorial, you must do the following:

  1. On MacOSinstallVisual Studio Code
  2. Is vs CodeinstallC + + extension。 You can install C / C + + extensions by searching for “C + +” in the extensions view (⇧ x).

Visual studio code debug C + + configuration under Mac and windows

Make sure clang is installed

Clang may already be installed on your Mac. To verify that it is correct, open the MacOS terminal window and enter the following command:

clang --version
  1. If clang is not installed, enter the following command to install the command-line developer tools:
xcode-select --install

Create project

1. Create a new folder, open it with vscode, and finish the project

Configure C + + compilation items

It is recommended to install the native debug plug-in directly. The official configuration method is cumbersome
Visual studio code debug C + + configuration under Mac and windows

debug

After the native debug plug-in is installed, you can directly press F5 to debug. The first time you start the plug-in, you can select the environment, select c + + (GDB / lldb) (see Figure 1-1), select configuration, and select clang + + – to generate and debug active files (see Figure 1-2)
Visual studio code debug C + + configuration under Mac and windows

Figure 1-1

Visual studio code debug C + + configuration under Mac and windows

Figure 1-2

Will be generated automatically tasks.json and launch.json
When compiling multiple files, you need to use the tasks.jso -Args adds the file path, and modifies the output file name, which cannot be the same as the file name under the folder. After the change, it is as follows:

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "*. CPP", // cpp file path
                "*. C", // C file path
                C path under "main / *. C", // main
                "-g",
                "-o",
                 //Cmaketest cannot have the same name as the current path
                "${fileDirname}/cmakeTest"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

Because the name of the generated program has been modified above, it needs to be modified synchronously launch.json The value of program in

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "Name": "clang + + - generate and debug active files,",
            "type": "cppdbg",
            "request": "launch",
            //And task.json  Output program file 1 to
            "program": "${fileDirname}/cmakeTest",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file"
        }
    ]
}

Windows tutorial

precondition

To successfully complete this tutorial, you must perform the following steps:

  1. installVisual studio code
  2. installVS CodeOfC / C + + extension。 You can install C / C + + extensions by searching for “C + +” in the extensions view (⇧ x).

Visual studio code debug C + + configuration under Mac and windows

3. Install mingw-w64 through SourceForge website. single clickMingw-w64To download windows mingw-w64 setup.

1. Run the installation program.
2. For architecture, choose x86_ 64, and then select next.
3. Next, use the default installation folder again and install MinGW.
  1. Use the following steps to add your mingw-w64binThe path of the folder is added to windowsPATHEnvironment variables:
1. In the windows search bar, type settings to open windows settings.
2. Search for the "Edit" environment variable for your account.
3. Select the 'path' variable, and then select Edit.
4. Select new and add the mingw-w64 path to the system path. The exact path depends on which version of mingw-w64 you installed and where you installed it. If you install mingw-w64 with the above settings, add it to the path: ` C: program files mingw-w64x86_ 64-8.1.0-posix-seh-rt_ v6-rev0mingw64bin`。
5. Select OK to save the updated path. You will need to reopen all console windows to make the new path location available.

Check your MinGW installation

To check that your mingw-w64 tool is properly installed and available, open a new command prompt and type:

g++ --version
gdb --version

If you don’t see the expected output org++perhapsgdbThere is no recognized command, please check your installation (Windowscontrol panel>program)And make sure that the – w64 binary location of your path entry MinGW matches at the compiler location.

Create project

1. Create a new folder, open it with vscode, and finish the project

Configure C + + compilation items

It is recommended to install the native debug plug-in directly. The official configuration method is cumbersome
Visual studio code debug C + + configuration under Mac and windows

debug

After the native debug plug-in is installed, you can directly press F5 to debug. The first time you start the plug-in, you can select the environment, select c + + (GDB / lldb) (see Figure 1-1), select configuration, and select clang + + – to generate and debug active files (see Figure 1-2)
Visual studio code debug C + + configuration under Mac and windows

Figure 1-1

Visual studio code debug C + + configuration under Mac and windows

Figure 1-2

Will be generated automatically tasks.json and launch.json
When compiling multiple files, you need to use the tasks.jso -Args join file path
The following is the compilation of CPP and C files in the current path and C files in the sub path

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe",
            "args": [
                "-g",
                "*.cpp",
                "*.c",
                "main/*.c",
                "-o",
                "${fileDirname}\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

Recommended Today

GPS timing products, NTP time calibration, time synchronization server, Beidou time service equipment

GPS timing products, NTP time calibration, time synchronization server, Beidou time service equipment GPS timing products, NTP time calibration, time synchronization server, Beidou time service equipment Long term production and supply of Beijing Zhun Electronic Technology Co., Ltd. ahjzsz summary NTP network time server is a high-tech clock product with high precision, large capacity and […]