g++ --version
gdb --version
可以看到输出结果
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
code .
命令可以让vscode在当前目录打开项目 。
新建 helloworld.cpp
输入下面代码后保存。
#include
using namespace std;
int main()
{
cout << "Hello world" <
使用命令行编译:
g++ helloworld.cpp -o helloworld.exe
按ctrl+shift+b
可选择编译器。 这里选择g++
创建 .vscode文件夹,并创建task.json文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
按 Ctrl+Shift+B
,选择G++编译:
当前目录下会生成helloword.exe。
可以按如下修改,编译所有CPP文件 并生成指定可执行的文件名:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"args": ["-g", "${workspaceFolder}\\*.cpp", "-o", "${workspaceFolder}\\a.exe"],
"options": {
"cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
点击菜单: Run > Add Configuration… , 选择 C++(GDB/LLDB)
这时在.vscode下会自动创建launch.json文件。
对文件进行适当 修改:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "指向自己的gdb.exe文件路径",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
按Ctrl+Shift+B编译程序,再按F5进入调试模式。