C++ | Vscode配置

文章目录

  • 配置文件
    • c_cpp_properties.json文件配置
    • launch.json文件配置
    • tasks.json文件配置
  • 修改配置参数
    • 解决VSCode调试C/CPP,CMD闪退
    • 将程序运行在外部控制台【推荐】
    • 收纳生成的 exe 可执行文件

配置文件

在.vscode文件夹下创建三个文件:c_cpp_properties.jsonlaunch.json、``

c_cpp_properties.json文件配置

  1. 复制下面的配置信息
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "c:/mingw/include/**",
                "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++",
                "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/mingw32",
                "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/backward",
                "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include",
                "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../include",
                "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include-fixed"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "c:/mingw/include/**",
                    "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++",
                    "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/mingw32",
                    "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/backward",
                    "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include",
                    "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../include",
                    "c:/mingw/bin/../lib/gcc/mingw32/8.2.0/include-fixed"
                ]
            }
        }
    ],
    "version": 4
}
  1. 打开cmd,搜索
 gcc -v -E -x c++ -
  1. 修改 “includePath” 和 “Path”,将图示这几行代替其中路径即可
    C++ | Vscode配置_第1张图片

launch.json文件配置

根据自己路径修改 "miDebuggerPath"

{  
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/exe/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            "miDebuggerPath": "D:/MinGW/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": [  
                {   
		    "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
}

tasks.json文件配置

{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${workspaceFolder}/exe/${fileBasenameNoExtension}.exe"
    ], // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "\\"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

修改配置参数

解决VSCode调试C/CPP,CMD闪退

上述代码以及修改过了:"program": "C:\\Windows\\System32\\cmd.exe",

将程序运行在外部控制台【推荐】

打开.vscode 文件夹下的 launch.json 文件,找到 "externalConsole": false, 将 false 改为true并保存。

收纳生成的 exe 可执行文件

  1. 打开.vscode 文件夹下的 task.json 文件,找到 "${fileDirname}\\${fileBasenameNoExtension}.exe"
  2. 修改成"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"并保存,同理,launch.json 下也有相同的字段,需要你修改
  3. 在源文件同目录下新建 bin文件夹,程序运行后,可执行文件将会生成在里面(其中 bin 可修改成你喜欢的英文名字)

你可能感兴趣的:(#,C++学习,vscode,c++,json)