VSCode configure C++

Every time you change computers to write C++, you have to find the configuration, which is very cumbersome. This time I wrote a blog and recorded the relevant configuration.

Install the compiler

Open the website below http://www.msys2.org/, download the 64-bit version MYSYS2, and follow the steps on the home page to install the software. After the completion, enter the /etc/pacman.ddirectory and modify the following three files respectively.

  1. The content of the mirrorlist.mingw32 file is:
    Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/

  2. The content of the mirrorlist.mingw64 file is:
    Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/

  3. The contents of the mirrorlist.msys file are:
    Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/

Then allow the following command to update the system.

cd ~
pacman -Syu

Install the c++ tools.

pacman -S mingw-w64-x86_64-toolchain

A prompt appears, select 3, 9, and 13 respectively. Wait for the installation to complete.

After completion, e:\Softwares\msys64\mingw64\bin\add to the system environment. Note: The installation e:\Softwaresdirectory is adjusted according to the actual situation.

Install VSCode

This time, we chose VsCode as the tool, which is convenient and practical. Although the C++ plug-in is a bit weak, the one developed by Microsoft will not be too bad. After the installation is complete, select c/c++Plugins . See below.

Open the software, then in the Filemenu, click Add Folder to Workspace..., select a directory as the folder for writing c++ code later, here we assume that the selected folder is cpp. Create a new directory under cpp , and .vscodethen create three files .vscodeunder the , directory, respectively . See below.task.jsonc_cpp_properties.jsonlaunch.json

tasks.cppThe content is as follows:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "command": "g++",
            "args": [
                "-g",
                " ${file}", //指定编译源代码文件                      
                "-o",
                " ${fileDirname}/${fileBasenameNoExtension}.exe", // 指定输出文件名,不加该参数则默认输出a.exe  
                "-ggdb3", // 生成和调试有关的信息  
                "-Wall", // 开启额外警告  
                "-static-libgcc", // 静态链接  
                "-std=c++17", // 使用最新的c++17标准  
                "-Wno-format",
                "-fexec-charset=GBK", //Console窗体输出字符编码 保证能正常显示中文  
                "-finput-charset=UTF-8" //输入编译器文本编码 默认为UTF-8  
            ],
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never  
                "focus": false,
                "panel": "shared" // 不同的文件的编译信息共享一个终端面板  
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "\\"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

c_cpp_properties.jsonContent:

{
    "configurations": [
        {
            "name": "Win32",
            "intelliSenseMode": "clang-x64",
            "includePath": [
                "${workspaceFolder}",
                "e:/Softwares/msys64/mingw64/include/c++/7.3.0/"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=7",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "e:/Softwares/msys64/mingw64/include/c++/7.3.0/"
                ]
            },
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": "",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 3
}

launch.jsonFor code debugging, it doesn't work yet.

run the code

After completing the above work, in the cpp directory, write a test.cpp file, and then press Ctrl+Shift+B, the test.exe file will be generated.

Guess you like

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