VS Code: build C / C ++ compiler debugging environment running


1. Background

  Usually are compiled using Visual Studio, debug and run C / C ++ program, compile it feels commissioning environment is good, but the body is too large, every time you start to spend a longer time. Only few dozen lines of source code, the size of the VS project to dozens of M, and the new project is relatively time-consuming. So now try to use the Visual Studio Code to build C / C ++ compiler debugging runtime environment to facilitate small C / C ++ program to program.
VS和VS Code

2. build step

Download and install the VS Code 2.1 Mingw-w64.

VS Code Official Download (https://code.visualstudio.com/Download)
mingw-W64 official download (https://sourceforge.net/projects/mingw-w64/files/mingw-w64/)

VS Code Download Options

mingw-w64 download options
Tip:
1. MinGW-W64-install.exe program is installed, click on the download can be installed, but ask your network better, or could not do.
2. i686 32-bit version, x86_64 64-bit version; os is win32 and POSIX interface type; sjlj, seh, dwarf exception handling scheme. Choosing the right operating system version can be, the other for beginners are not very different. After downloading copy directly extract the location you want to install to, note the path without spaces or Chinese.
3. MinGW-W64 need to configure the environment. My Computer> Right select Properties> click Advanced System Settings> click Environment Variables> Click the System Variables path> add the bin address MinGW-W64 (C: \ mingw64 \ bin; ). Remember to click OK to save the settings, use "gcc -v" test has been installed MinGW-W64

System Variables path path

MinGW-W64 installation test results

2.2 Installation VS Code plug-ins.

VS Code plug-ins

Plug use
C/C++ To build C / C ++ environment necessary
C++ Intellisense IntelliSense plugin supports syntax highlighting part, optional
Chinese (Simplified) Language Pack for Visual Studio Code Speaking vs code plug, optional
Code Runner Run the program shortcuts plug, optional
Path Autocomplete Path automatically prompted to plug, optional

3. Code Debugging

  1. Open the folder, the new 1.cpp.
    Open the folder

New 1.cpp

#include <iostream>

int main()
{
    int a = 9;
    std::cout << a << std::endl;
    std::cout << "hello" << std::endl;
    std::cout << "你好" << std::endl;
    return 0;
}
  1. Open 1.cpp, click on the "Debug"> "start debugging." Select " C ++ (GDB / LLDB) ", select " G ++. EXE Build and Debug the Active File ", so you can automatically create launch.json.

Start Debugging

Select C ++ (GDB / LLDB)

选择g++.exe build and debug active file

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}
  1. Open 1.cpp, click on the "Debug"> "Start Debugging" ( Do not debug on launch.json ). At this point the system error, click on the " Configuration Tasks ", select " G ++. Build the Active EXE File ." So the system will automatically create tasks.json.
    If " G ++. Build the Active EXE File " does not exist, probably because you debug launch.json, can be converted to 1.cpp re-commissioning.

System error

g++.exe build active file

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}
  1. Finally, open 1.cpp, click on the "Debug"> "Start Debugging" to commissioning.

Debugging results

4. Run the code (3 methods)

4.1 running in non-debug mode (Ctrl + F5)

image.png

operation result

Note: which encoding scheme is UTF-8. If replaced GB2312, it may not be output Chinese.

Chinese GB2312 can not be output

4.2 g ++ command terminal operation

g++ 1.cpp -o 1.exe; ./1.exe

terminal

operation result

Note: its encoding is GB2312. If replaced UTF-8, the output of Chinese will be garbled.

Chinese garbled

4.3 Code Runner mounting plug (red rectangle in the drawing) Press the run key

Code Runner
operation result

Note: its encoding is GB2312. If replaced UTF-8, the output of Chinese will be garbled.

Chinese garbled

5. Summary

  1. Familiar VS Code can not be achieved, when only the built environment to build a basic environment is good, be able to run C / C ++ programs can be compiled debugging, multi-purpose or without it added too many plug-ins installed. VS Code after using more than there is demand, naturally know how to configure the environment, such as shortcuts do not need to remember how, with more naturally know.
  2. There are many online c_cpp_properties.json, tasks.json, launch.json, the focus is read again. For starters, the system automatically vivid tasks.json and launch.json have basic enough.
  3. For VS Code Chinese garbled in doubt, refer to VS Code: 4 Ge Chinese garbled problems and solutions .

6. References

  1. Ask: win10 64 bits should be installed which version of MinGW
  2. Visual Studio Code (vscode) configuration C, C ++ environment / write operation C, C ++ (mainly Windows, brief Linux)
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103758571