Windows configuration vscode c/c++ development (tend to brush questions)


process

  1. Install vscode
  2. MinGW-w64-for 32 and 64 bit Windows scroll down a bit and find MinGW-W64 GCC-5.4.0the x86_64-posix-sehinstallation. Why this version? Because the two C++ compilers supported by Niuke are respectively C++14(G++5.4.0)and C++11(glang 3.9), here is guaranteed to be the same as Niuke
  3. Unzip the MinGW archive to the path you want to install. Add the bin directory to the environment variable path:
    Insert picture description here
    Operating systems below win10 need to be restarted to take effect.
  4. Install the vscode plugin:
    • C/C++: also known as cpptools, provides Debug and Format functions
    • Code Runner: Right-click to compile and run a single file, which is very convenient; but it cannot be Dubug
  5. New projects, new in the project directory .vscode folder, create a new folder in launch.json, tasks.json, c_cpp_properties.jsonfile:
    Insert picture description here
    launch.jsonContent:
// launch.json
{
    
    
    "version": "0.2.0",
    "configurations": [
        
        {
    
    
            "name": "C++ Launch (GDB)",                 // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",                           // 配置类型,这里只能为cppdbg
            "request": "launch",                        // 请求配置类型,可以为launch(启动)或attach(附加)
            "targetArchitecture": "x64",                // 生成目标架构,一般为x86或x64
            // "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            // "args": [],                                 // 程序调试时传递给程序的命令行参数,一般设为空即可
            // 方法一:防止debug时程序一闪而过的方法
            // "program": "C:/Windows/System32/cmd.exe",
            // "args": [
            // "/K",
            // "${fileDirname}/${fileBasenameNoExtension}.exe"
            // ],
            // 方法二:
            "program": "C:/Windows/System32/cmd.exe",
            "args": [
                "/C",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "&",
                "pause"
            ],

            
            "stopAtEntry": false,                       // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceRoot}",                  // 调试程序时的工作目录,一般为${workspaceRoot}
            "externalConsole": true,                    // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "internalConsoleOptions": "neverOpen",      // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡",
            "MIMode": "gdb",                            // 指定连接的调试器
            "miDebuggerPath": "C:/mingw64/bin/gdb.exe", // 调试器路径
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for GDB",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" 
        }
    ]
}

tasks.jsoncontent:

// tasks.json
{
    
    
    "version": "2.0.0",
    "tasks": [
        {
    
    
            "label": "Compile",
            "command": "g++",
            "args": [
                "${file}",   // 指定编译源代码文件
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe", // 指定输出文件名,不加该参数则默认输出a.exe
                "-O0",      // 优化级别
                "-ggdb3",   // 生成和调试有关的信息
                "-Wall",    // 开启额外警告
                "-static-libgcc",   // 静态链接
                //"-std=c++17",       // 使用c++17标准
                "-std=c++14",       // 使用c++14标准
                "-finput-charset=UTF-8",    //输入编译器文本编码 默认为UTF-8
                "-fexec-charset=GB18030",   //输出exe文件的编码
                "-D _USE_MATH_DEFINES"
            ],

            "problemMatcher": {
    
    
                "owner": "cpp",
                "fileLocation": [
                    "absolute",
                ],
                "pattern": {
    
    
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },

            "type": "shell",
            
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            },

            "presentation": {
    
    
                "echo": true,
                "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
                "focus": false,
                 "panel": "shared" // 不同的文件的编译信息共享一个终端面板
            },
        }
    ]
}

c_cpp_properties.jsoncontent:


{
    
    
    "configurations": [
        {
    
    
            "name": "MinGW64",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:/mingw64/bin/g++.exe",
            "includePath": [
                "${workspaceFolder}"
            ],
            "cppStandard": "c++14"
        }
    ],
    "version": 4
}

other problems

  • C++ flashed after pressing F5 to execute the program:
    Solution: Modify program and args in launch.json
    Insert picture description here
    Method One Principle:
    Realize through cmd parameter "/K"
    Means: Do not close the command window after executing the command
    Method Two Principle:
    This method is mainly realized by the cmd parameter "/C", and its effect is: "execute the command specified by the string and then terminate". In addition, you can use "help cmd" to view all available parameters.

EVERYTHING:

  • Improve and simplify the process
  • Display the time when the program is finished (not including the time to enter the sample)
  • It is best to automatically read test sample files
  • The number of characters entered in the vscode console is limited. Test cases that are too long cannot be entered. Try to redirect operations such as cin.

Related/reference links

VSCode C++ exits immediately after the end of running, the window flashes the solution, do not modify the code | At the beginning of the article, I also gave a few official Microsoft tutorials to teach you how to configure the c++ environment on vscode
Get Started with C++ and Mingw-w64 | Official tutorial And teach you how to debug

How to write and run C and C++ programs in Visual Studio Code? -Tan Jiuding's answer-Knowing
Visual Studio Code -> VSCode development environment setup ---- C/C++ development environment setup and code running (code runner plug-in)
vscode for c/c++ (ACM configuration)

Guess you like

Origin blog.csdn.net/a435262767/article/details/105850685