VSCode C++ 开发环境搭建

我未来会大量写C++,因此需要一个顺手的IDE,但是VS Studio过于臃肿,并且linux下还没法使用,DEV过于简陋。权衡过后选择了VSCode,但Linux下仍会使用Clion。以下记录VS Code C++开发环境搭建流程。

Reference

官方说明

Video by Coding Tech(In cppcon 2017)

Variables Reference

https://github.com/RebornHugo/My_VSCode_ConfigFile

VSCode extensions 上对两个扩展插件的说明

本文是在上述资料之上的总结与扩充,便于我个人快速回忆

Platform

A

  • Win10 1803
  • Version 1.24.0

B

  • Ubuntu 16.04
  • Version 1.24.0

Get Started

Windows10

以下先说明win10下环境搭建,linux上更常用Clion。

Two Extensions Usage

首先安装两个extensions

1528717764647

With the C/C++ extension installed, open a folder that contains your C/C++ source code. VS Code will place various settings files into a .vscode subfolder.

这里的.vscode文件夹是编译运行Cpp文件的关键。1528718042996

The C/C++ extension does not include a C++ compiler or debugger. You will need to install these tools or use those already installed on your computer. Popular C++ compilers are mingw-w64 for Windows, Clang for XCode for macOS, and GCC on Linux. Make sure your compiler executable is in your platform path so the extension can find it. The extension also supports the Windows Subsystem for Linux.

也就是说我们需要自己安装mingw或者GCC。由于之前装过DEV Cpp,找到目录下的MinGW64下bin文件夹,添加路径到环境变量。

1528718840301

配置成功后可以在bash下测试gcc命令,发现可以使用。


然后配置IntelliSense,用于Provide Intellisense for C/C++ with the help of the GNU Global tool in Visual Studio Code. 它会从系统上找到编译器并试图基于此编译器来决定文件夹的基本配置信息。如果有误,可以自行在控制面板产生***c_cpp_properties.json补充或修改。

以下是一个正常的c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "${workspaceFolder}"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\Coding\\Dev-Cpp\\MinGW64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

Building your code

要构建应用程序,需要先产生tasks.json,注意选取Others模版

以下是一个正确的tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "type": "shell",
            "command": "g++",
            "args": [
                        "-g", "\"${file}\"","-o",
                    ],
            "windows": {
                "args": [
                    "\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
                ]
            },      
            "presentation": {
                "reveal": "always",
                "panel": "shared",
                "focus": false,
                "echo": true
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
  ]
}

这里的command+args实现了通配。无需输入文件名和生成的文件名。${file}会找到当前打开的文件的根路径。更多参见Variables Reference

The task’s properties have the following semantic:

  • label: The task’s label used in the user interface.
  • type: The task’s type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If processis specified, the command is interpreted as a process to execute.
  • command: The actual command to execute.搭配args一起使用
  • group: Defines to which group the task belongs. In the example, it belongs to the build(自己定义的label) group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette. isDefault告诉VSCode这是defualt build task.
  • presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.
  • problemMatcher: 将terminal中的报错信息返回到problems处.

Debugging

1528723316830

点击这里创建lanuch.json,windows选择(gdb) Launch,生成launch.json

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            // "program": "\"${workspaceFolder}\\${fileBasenameNoExtension}.exe\"",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Coding\\Dev-Cpp\\MinGW64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Compile",
        }
    ]
}

这里关键是配置program的路径和miDebuggerPath的路径。然后设置断点即可开始debug了。”preLaunchTask”指debug前会重新编译“Compile”这个task。

hackerHugo wechat
一个一万年没有更新的公众号