Use vscode to compile and debug C/C++ under Linux

1. Installation

The environment is Ubuntu gnome 16.10. It can be installed as follows:

  1. Install popular IDEs using ubuntu-make. The command isumake ide visual-studio-code
  2. Download the deb package from Microsoft's official website and install it by yourself.

2. Configuration

  1. Install the c/c++ plug-in (Microsoft official plug-in) to support the cpp language .
    You can directly search for installation or go to the official website plug-in library to install; you can also enter the command after Ctrl+Pext install c++ Install the plugin
  2. The system needs to install the compilation and debugging environment (gcc, g++). Build-essential can be installed directly under Ubuntu
  3. Project configuration
    Open the project folder with VSCode, open a source file, direct shortcut keys ctrl + shift + D, click the settings icon, select C++ (GDB/LLDB) in the pop-up selection, and the project's launch.json file ( official document ) will be created automatically. ), the default is the debug configuration. But why not run configuration?
  • Modify the value of the program field and change it to the executable file path generated by compilation. eg "program": "${workspaceRoot}/${fileBasenameNoExtension}.out". That is, if the source file is case.c, the case.out file will be debugged.
  • But at this time, the F5 debugging operation will not find the executable file, and the compilation configuration must be done. Add a task option to launch.json: "preLaunchTask": "build". After saving, switch to the source file, press F5 to debug, and a message box will pop up asking to configure the task runner. After selecting, click Others, jump out of the tasks.json ( official document ) configuration file, and configure a task named "build".
{
    "version": "0.1.0",
    "command": "gcc",  // 编译C程序。可以换成 echo 来调试命令参数
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "build",  // 任务名
            "suppressTaskName": true,
            "args": ["-g", "-o", "${fileBasenameNoExtension}.out", "${file}"],  // 命令参数
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation":  ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

3. Compile and debug

After saving, switch to the source code and press F5 again to start debuggingdebugging

4. Reference

  1. How to compile and run C++ in VSCode?
  2. Compile and debug C++ projects with VS Code in Linux
  3. official documentation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325537927&siteId=291194637