VScode配置g++编写c++

  1. 将g++添加到环境遍历
  2. 安装插件”code runner”
  3. 设置-插件-run code configuration-Run in terminal 勾选上(否则如果程序里有输入等待将无法继续)
  4. 下载微软官方插件C/C++ 以便于调试
  5. 在目录下的.vs文件夹里面生成:launch.json/tasks.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            
            
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            
            
            //"program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            //"miDebuggerPath": "/path/to/gdb",
            "miDebuggerPath": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe",
            "preLaunchTask": "g++",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

 

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-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
    }
    }
}
  •  若引用”bits/stdc++.h报错。可能是因为默认complier是clang,在用户设置里更改,添加一句:
    "C_Cpp.default.compilerPath": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe",

    就好了。

  • .vs文件夹可以放在常用目录的根部,对所有子目录均可用
  • 若要快速建立配置文件,可以自己生成一个用户代码片段
    {
    	// Place your snippets for jsonc here. Each snippet is defined under a snippet name and has a prefix, body and 
    	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    	// same ids are connected.
    	// Example:
    	 "c++tastks": {
    	 	"prefix": "c++tasks",
    	 	"body": [
    	"\"version\": \"2.0.0\",\r\n\"command\": \"g++\",\r\n\"args\": [\"-g\",\"\\\\\\${file}\",\"-o\",\"\\\\\\${fileDirname}/\\\\\\${fileBasenameNoExtension}.exe\"],\r\n\"problemMatcher\": {\r\n\"owner\": \"cpp\",\r\n\"fileLocation\": [\"relative\", \"\\\\\\${workspaceRoot}\"],\r\n\"pattern\": {\r\n\"regexp\": \"^(.*):($1\\d+):($1\\d+):$1\\s+(warning|error):$1\\s+(.*)$\",\r\n\"file\": 1,\r\n\"line\": 2,\r\n\"column\": 3,\r\n\"severity\": 4,\r\n\"message\": 5\r\n}\r\n}"
        
    	 	],
    	 	"description": "c++ tasks.json"
    	 },
    	 "c++l1": {
    		"prefix": "c++l1",
    		"body": [
    			"\r\n\"miDebuggerPath\": \"C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe\",\r\n\"program\": \"\\\\\\${fileDirname}/\\\\\\${fileBasenameNoExtension}.exe\",\r\n\"preLaunchTask\": \"g++\"\r\n"
    		],
    		"description": "c++ launch.json"
    	}
    }

     

发表评论

电子邮件地址不会被公开。 必填项已用*标注