vscode C++环境配置(mingw-w64 和 GDB)

vscode的3个配置文件说明。 VS需要为每一个文件夹做单独配置,所以建议把.vscode文件夹放到你常用的文件夹的顶层,这样就不用重复配置了。 不用每个新cpp文件就要一套配置。这些配置在你配置好的文件夹内的所有子文件夹和文件都能使用。  
  • task.json
task.json文件告诉VS Code如何构建(编译)程序。这个任务将调用g++编译器来创建一个基于源代码的可执行文件。 Terminal > Configure Default Build Task.
{
    "version": "2.0.0",
    "command": "g++",                                                       // label,可任取
    "args": [                                                               // g++命令行参数
        "-g",
        "${workspaceFolder}\\*.cpp",
        "-I",
        "--std=c++11",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
    ], 
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceFolder}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

 

  • launch.json
launch.json文件配置VS Code启动GDB调试器。 通过智能提示查看有那些属性可以设置, 如果要查看属性的具体含义, 可以把鼠标悬停在属性上面, 会属性的使用说明.
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",                                                                                    // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",                                                                                          // 配置类型,这里只能为cppdbg
            "request": "launch",                                                                                       // 请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",                                                // 将要进行调试的程序的路径
            "args": [],                                                                                                // 程序调试时传递给程序的命令行参数,一般设为空即可
            "stopAtEntry": false,                                                                                      // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceFolder}",                                                                               // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
            "environment": [],
            "externalConsole": true,                                                                                   // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/gdb.exe",      // miDebugger的路径,注意这里要与MinGw的路径对应
            "preLaunchTask": "g++",                                                                                    // 调试开始前执行的任务,要与task.json里的label/command值一样
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        }
    ]
}
 

 

  • c_cpp_properties.json
c_cpp_properties.json 对C/ C++扩展进行更多控制,允许相关改变设置,例如编译器的路径,包括路径,c++标准(默认是c++ 17),等等。
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [                                                            // 头文件包含路径
                "${workspaceFolder}/**",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/include/**",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/include/**",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

 

  • vscode预定义的变量 
e.g.:  
1. vscode编辑器打开的文件:/home/your-username/your-project/folder/file.ext
2. vscode打开的根工作区:/home/your-username/your-project
 
${workspaceFolder}           - 在VS Code中打开的文件夹路径                                     - /home/your-username/your-project
${workspaceFolderBasename}   - 在VS Code中打开的文件夹名称没有任何斜杠(/)                       - your-project
${file}                      - 当前打开的文件                                                  - /home/your-username/your-project/folder/file.ext
${fileWorkspaceFolder}       - 当前打开的文件所在的文件夹                                       - /home/your-username/your-project
${relativeFile}              - 相对于workspaceFolder,当前打开的文件                           - folder/file.ext
${relativeFileDirname}       - 相对于workspaceFolder,当前打开的文件的目录名                    - folder
${fileBasename}              - 当前打开的文件的基名                                            - file.ext
${fileBasenameNoExtension}   - 当前打开的文件的基名没有文件扩展名                               - file
${fileDirname}               - 当前打开文件的目录名                                            - /home/your-username/your-project/folder
${fileExtname}               - 当前打开的文件的扩展名                                          - ext
${cwd}                       - 任务运行器启动时的当前工作目录                                   
${lineNumber}                - 活动文件中当前选定的行号
${selectedText}              - 活动文件中当前选定的文本
${execPath}                  - 运行VS Code可执行文件的路径
${defaultBuildTask}          - 默认生成任务的名称
${pathSeparator}             - 操作系统用于分隔文件路径中的组件的字符                            - / on macOS or linux, \\ on Windows
 

  

参考: 1.官网:https://code.visualstudio.com/docs/cpp/config-mingw#_prerequisites 2.博客:https://blog.csdn.net/bat67/article/details/76095813
上一篇:Windows 下 MSYS2 环境配置和 MinGW-w64 C++ 环境配置


下一篇:可执行程序的编译过程