使用vscode+MinGW进行简单的编写代码(平时代码学习)
使用vscode+MinGW进行简单的编写代码(平时代码学习)
平时编程学习,没有必要涉及到使用CMake来构建项目。如下图所示,C++Primer表示工程目录,下面的chapter7或chapter8或cpptest表示项目目录。
C++Primer目录下的.vscode目录是作为工程配置文件,用来配置运行代码的一些配置信息。
比如在cpptest目录下,新建了3个文件main.cpp、swap.cpp和swap.h。现在需要进入main所在的代码文件下面,运行代码。在运行前需要对.vscode文件夹下的配置文件进行配置。
各配置文件的信息如下所示:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\application\\minGW\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"main.cpp",
"swap.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: D:\\application\\minGW\\mingw64\\bin\\g++.exe"
}
]
}
因为cpptest目录下面涉及多个文件,所以属于多文件编译,所以在args这一项指定编译哪些文件。
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) Launch",
"preLaunchTask": "g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
"type": "cppdbg",//配置类型,只能为cppdbg
"request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
"args": [],//调试传递参数
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,//true显示外置的控制台窗口,false显示内置终端
"MIMode": "gdb",
"miDebuggerPath": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
settings.json
{
"files.associations": {
"ostream": "cpp"
}
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\application\\minGW\\mingw64\\bin\\g++.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
接下来就开始运行了。点击右侧的小三角按钮边上的下拉列表,选择调试或运行即可。