【简洁易懂】之VSCode编译配置C++环境

一、修改配置UI

按Ctrl + Shift + P打开命令调面板,并输入C/C++,并点击编辑配置”(UI)。

  • 指定编译器路径或从下拉列表中选择检测到的编译器路径: C:\msys64\mingw64\bin\gcc.exe
  • IntelliSense 模式: windows-gcc-x64

二、构建任务

创建一个tasks.json来告诉vscode如何编译程序.

  • 点击cpp文件
  • Ctrl + Shift + P打开命令调面板,并输入tasks.
  • 点击配置默认生成任务
  • C/C++:g++.exe build active exe
  • 按快捷键ctrl+shift+B,即可生成helloworld.exe等文件(表示成功)
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++.exe 生成活动文件",
			"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "编译器: C:\\msys64\\mingw64\\bin\\g++.exe"
		},
	]
}

三、调试debug模式

创建一个launch.json文件

  • 调试
  • 添加配置
  • 选择第二个C / C++Windows(Launch)
  • 点选g++.exe…
  • 将自动创建和打开文件launch.json
{
    "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": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ],
        "preLaunchTask": "C/C++: g++.exe build active file"
      }
    ]
  }

调试代码文件时如果修改了代码,需要到tasks.json里面ctrl+shift+B重新生成 name.exe才能显示更新的代码结果。

(以后创建新的c++只需要把这次配置的.vscode文件夹复制过去,修改task.json和launch.json中跟项目工程名字相关内容即可)

你可能感兴趣的:(C++,vscode,c++,ide)