1.下载便携式MinGW软件包
1.1下载codeblock的步骤
我们下载的安装包中有MinGW,我们接下来部署编译环境需要用到这个目录内的文件
2.将编译路径添加到环境变量
在Windows11中添加环境变量的地方在高级系统设置这里
2.1将MinGW的bin路径添加到环境变量中
D:\software\codeblock\codeblocks-20.03mingw-nosetup\MinGW\bin
D:\software\codeblock\codeblocks-20.03mingw-nosetup\MinGW\include
点击环境变量
添加环境变量
最后添加完环境变量点击确定
3.在CMD中配置环境变量
输入gcc -v -E -x c++ -并且显示如下则配置成功
4.配置vscode
首先是我们三个配置文件(参考:Vs code写C语言代码配置(超详细超基础)_vscode c-CSDN博客)
(1)c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:\\software\\codeblock\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\g++.exe", /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
(2)launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"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": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\software\\codeblock\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\gdb.exe", /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++"
}
]
}
(3)tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "D:\\software\\codeblock\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\project\\Datestruct\\helloworld",
"-std=c++17"
],
"options": {
"cwd": "D:\\software\\codeblock\\codeblocks-20.03mingw-nosetup\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\software\\codeblock\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
5.在开发时遇到的vscode的一些问题
1.控制台出现乱码
因为我们使用cmd控制终端,它默认的是GBK,我们将代码改成GBK就可以
【vscode调试配置、输出中文乱码解决】关于VSCode的C++原生断点调试debug和运行的配置和中文乱码的问题(非code runnner)_vscode中文乱码怎么解决-CSDN博客
2.控制台一闪而过
在return前加上system("pause");
3.每次打开文件乱码
例如我们的目标文件是GBK格式编码,但是vscode默认的编码是utf-8,我们只需要做如下修改即可
(打开文件首选项编码即可)