安装扩展(extension)
- C/C++:又名 cpptools,提供Debug和Format功能
- Code Runner:右键即可编译运行单文件,但无法Dubug
第一个坑, 找不到gdb64.exe文件
下载TDM-GCC5.1后默认的安装选项竟然不选gdb,这是什么鬼,安装说明文档中有这么一段话:
You'll need GDB particularly if you want to use an IDE with debugging support.
好吧,重新安装,选中这个选项
另外安装程序有将gcc加入到环境变量PATH的选项,这点倒不用我们操心了。
第二个坑,配置各种json文件
由于我更改默认安装路径到D:\TDM-GCC-64,需要更改几个配置文件,先新建一个文件夹,比如F:\VS-Code-C,打开VScode,在其中打开这个文件夹,新建一个.vscode文件夹,在其中新建launch.json文件,拷贝内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
// 要进行调试的可执行文件的路径
"program": "${workspaceRoot}/main",
// gdb调试器路径,这里选择TDM-GCC安装目录下的gdb
"miDebuggerPath":"D:/TDM-GCC-64/bin/gdb64.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
// 调试会话前执行的任务,这里是task.json中配置好的编译任务
"preLaunchTask": "g++",
// pretty-printing配置,可以直观打印出C++中的容器
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
}
]
}
重点修改"miDebuggerPath"项,目录层级用/隔开。
新建并配置tasks.json
{
"version": "2.0.0",
"command": "g++",
"args": ["-g","${file}","-o","${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
}
}
}
第三个坑, c_cpp_properties.json的配置,
首先是"includePath"和"compilerPath" ,这个照例改为自己GCC的安装路径
另外还要注意的两点,配置不好会显示两个问题:
第一:IntelliSense模式msvc-x64与编译器路径不兼容
这是因为msvc-x64对应的编译器是Windows中的Visual Studio,咱们编译器没有选择Visual Studio,所以IntelliSense 模式不兼容,此处,选择和g++ on Mingw-w64兼容的IntelliSense 模式模式就可以了,此处选择:gcc-x64,
————————————————
{
"configurations": [
{
"name": "Win32",
"includePath": [
"D:/TDM-GCC-64/include",
"D:/TDM-GCC-64/x86_64-w64-mingw32/include",
"D:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include",
"D:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++"
],
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"intelliSenseMode": "gcc-x64", //默认为msvc-x64
"compilerPath": "D:\\TDM-GCC-64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
//"clang_format": 在这里设置, 现在先删除
"version": 4
}
第二:property clang_format is not allowed
目前还用不上,删掉下面的设置即可
"clang_format": {
"style": "file",
"fallback-style": "LLVM",
"sort-includes": false
},
下面新建一个main.cpp,在代码界面点击右键,选择Run Code (前面安装的扩展), 即可在终端中显示结果。否则,必须在第七行下断点,不然运行结果会一闪而过。
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Vscode" << endl;
return 0;
}