Visual Studio Code configuration C, C + + runtime environment

Source: Internet
Author: User
Tags windows download



System environment: 64-bit Windows 10


1. Preparation of the environment


(1), download mingw-w64


    • Mingw-w64-for and the bit Windows download mingw-w64 on this page, click Download
MinGW, MINGW-W64, they are two different projects, MinGW has not been updated for a long time, not recommended (2), add environment variables, my computer------Advanced system settings, environment variables----System variables-- Add mingw-w64 Installation Path (3), run cmd, input gcc-v display the corresponding version (4), need to install the plugin:
    • C + + (Cpptools in some tutorials)
    • Code Runner: Run the selected snippet (supports a large number of languages, including node)
    • Include Autocomplete: Provide header file completion
    • Snippets:snippets code blocks are reused


Other optional plugins:


    • Bracket Pair Colorizer: Rainbow curly Braces
    • One Dark Pro: Probably the most high-volume vs. code installation topic
    • GBKtoUTF8: Converts a GBK encoded document into a UTF8 encoded
    • C + + Clang Command Adapter: Provides static detection (Lint)
2. Configure four. json files


(1) Create a folder (called a workspace) where you plan to store your code, and the path cannot contain Chinese or white space



(2), open vs Code, select Open folder, select just that folder, point vs Code on the new folder, the name is . Vscode(This is done because Windows Explorer does not allow the creation of folders the first character is a dot)



(3), create the following 4 files into the. Vscode folder, as shown in the effect:



Launch.json



Tasks.json



Settings.json



C_cpp_properties.json



(4), Launch.json file, add the following code:
"Midebuggerpath": "D:\\mingw-w64\\mingw64\\bin\\gdb.exe", you need to modify the path to the mingw-w64 you installed
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CppDebug",
            "type": "gdb",
            "request": "launch",
            "target": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "CppDebug" // The task to be executed before the debugging session starts, usually the compiler. Corresponds to the label or taskName of tasks.json
        },
        {
            "name": "C++ Launch", // configuration name, will be displayed in the debug configuration drop-down list
            "type": "cppdbg", // Debugger type: Windows specifier uses cppvsdbg; GDB and LLDB use cppdbg. This value is automatically generated
            "request": "launch", // debugging mode
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // Program to be debugged (full path, relative path supported)
            "args": [], // passed to the parameters of the above program, no parameters can be left blank
            "stopAtEntry": false, // whether to stop at the program entry point (stop at the start of the main function)
            "cwd": "${workspaceRoot}", // working directory when debugging the program
            "environment": [],
            "externalConsole": true, // whether to display the console window during debugging
            "preLaunchTask": "LaunchBuild",
            "linux": { // The following are the parameters that need to be configured under the Linux platform.
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            "osx": { // The following are the parameters that need to be configured on the Mac platform.
                "MIMode": "lldb"
            },
            "windows": { // The following are the parameters that need to be configured under the Windows platform.
                "MIMode": "gdb", // Debugging tool to be used by VSCode
                "miDebuggerPath": "D:\\mingw-w64\\mingw64\\bin\\gdb.exe", // The path to miDebugger, this value must be set. Although it will automatically search
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        },
        { // Don't care
            "name": "C++ Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "processId": "${command:pickProcess}", // the process ID of the Attach
            "linux": {
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            "osx": {
                "MIMode": "lldb"
            },
            "windows": {
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        }
    ]
} 


(5),tasks.json file, add the following code:


{
    "version": "0.1.0",
    "tasks": [
        {
            "taskName": "LaunchBuild", //task name
            "command": "g++",
            "isBuildCommand": true, //compile, shortcut key crtl+shift+B, need this identifier
            "args": [
                "-fexec-charset=GBK", // support Chinese display
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ], // compile command parameters
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "taskName": "CppDebug", //task name, debug debugging, shortcut key: F5
            "command": "g++",
            "args": [
                "-fexec-charset=UTF-8",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
} 


(6),settings.json file, add the following code:



Code Runner command line and some options can be modified here according to their own needs, the following code to adjust according to their own situation, after writing a comma, the last one will not be



This setting is used in the global Settings.json (this file can be empty if not required)


{
     "editor.fontFamily": "Consolas, Microsoft Yahoo", // Control Editor Font
     "workbench.colorTheme": "One Dark Pro", // theme
     "files.trimTrailingWhitespace": true, // delete the space at the end of each line when saving
     "workbench.colorCustomizations": {
         "activityBar.foreground": "#33ff66" // custom color
     },
     "git.enabled": false, // if you don't use git, I suggest you close it
     "editor.minimap.enabled": false, // I don't need a minimap, it's the one on the right
     "editor.dragAndDrop": false, // Once the text is selected, you can drag them to adjust the position. I don't need it
     "files.autoGuessEncoding": true // When enabled, attempts to guess character set encoding when opening a file
} 


(7),C_cpp_properties.json file, add the following code:



my version is 7 and 7.2.0, if it is different from me, you need to modify the path in the file (must be modified to mingw-w64 installation path), otherwise you will be prompted to find the header file;



Enter Gcc-v to view the version number



The path under Windows is a backslash and should have been escaped with two backslashes, but it is also accepted in VS code directly with a slash


 
 
{
    "configurations": [
        {
            "name": "Win32",
            "intelliSenseMode": "clang-x64",
            "includePath": [
                "${workspaceFolder}",
                "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                "D:/mingw-w64/mingw64/include",
                "D:/mingw-w64/mingw64/x86_64-w64-mingw32/include",
                "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include-fixed"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=7",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                    "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                    "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                    "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                    "D:/mingw-w64/mingw64/include",
                    "D:/mingw-w64/mingw64/x86_64-w64-mingw32/include",
                    "D:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include-fixed"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 3
}
}


(8), mode



Directly press the shortcut key F5, you can be debugged



There is a problem: "System (" pause ") appears in the program; "For debugging, the debug console in VS code is garbled, displaying characters that are??????????? . . .



Workaround: Comment System ("pause"); does not affect debugging (does not know how to change the encoding format of the debug console for VS code)






(9), compile, run



You need to edit it before you run it.



1, compile, press the shortcut key crtl+shift+b, compile



2, operation, operating as follows:









3, if you have done debugging before, need to compile again, then run, otherwise it will produce Chinese garbled



Cause: Debug F5 generated by the encoding format of the running file is UTF-8, in the terminal, Chinese display garbled






(10), Permanent Resolution CMD window, Chinese display garbled






1. win+r Enter regedit to enter the registration form
2. Find Hkey_current_user\console\%systemroot%_system32_cmd.exe If the codepage item already exists under this key, change the value to decimal "65001", or if it does not exist, create a new DWORD (32-bit value), named "CodePage", with the value set to "65001"
3. Effective after restarting CMD
4. For power shell modifications, just modify the 2nd step
The item under%systemroot%_system32_windowspowershell_v1.0_powershell.exe.



Visual Studio Code configuration C, C + + runtime environment


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.