配置Windows下vscode的GCC开发环境
1 安装环境
1.1 安装vscode
1.2 安装chocolate
以管理员身份运行Powershell, 执行以下命令:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
1.3 安装MinGW-w64
管理员权限运行Cmd 或者 powershell ,执行
choco install mingw -y
1.4 安装ms-vscode.cpptools插件
2 添加vscode配置
2.1 创建.vscode文件夹
- 在根目录下创建.vscode文件夹
2.2 添加c_cpp_properties.json
- c_cpp_properties.json 配置了代码的智能语法辅助功能
- intelliSenseMode 要配置成 gcc-x64
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"forcedInclude": [],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compileCommands": "",
"compilerArgs": [],
"compilerPath": "c:\\ProgramData\\chocolatey\\bin\\gcc",
"cStandard": "c17",
"cppStandard": "c++17",
"customConfigurationVariables": {},
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:\\ProgramData\\chocolatey\\lib\\mingw\\tools\\install\\mingw64\\lib",
"C:\\ProgramData\\chocolatey\\lib\\mingw\\tools\\install\\mingw64\\include"
],
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"windowsSdkVersion": "10.0.18362.0",
"macFrameworkPath": []
}
],
"version": 4
}
2.3 添加 launch.json
- launch.json 配置了gdb的启动任务
- preLaunchTask 配置了启用gdb之前,会执行 C/C++: g++.exe build active file 任务。
{
// 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": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\ProgramData\\chocolatey\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
2.4 添加tasks.json
- tasks.json 配置了名为 C/C++: g++.exe build active file 的gcc编译任务
- 注意默认的Terminal不要选bash,会导致${file}路径不正常
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"problemMatcher": "$gcc",
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
2.5 添加settings.json
- settings.json 规定c/cpp使用了ms-vscode.cpptools插件,而json使用vs自带的vscode.json-language-features,注意配置文件是json的
- 附带了一些编辑器配置,可以根据个人情况删减
{
"[c]": {
"editor.defaultFormatter": "ms-vscode.cpptools",
},
"[c++]": {
"editor.defaultFormatter": "ms-vscode.cpptools"
},
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"C_Cpp.autoAddFileAssociations": true,
"C_Cpp.autocomplete": "Default",
"C_Cpp.clang_format_fallbackStyle": "Google",
"C_Cpp.clang_format_path": "",
"C_Cpp.clang_format_sortIncludes": true,
"C_Cpp.clang_format_style": "file",
"C_Cpp.codeFolding": "Enabled",
"C_Cpp.commentContinuationPatterns": [
"/**"
],
"C_Cpp.configurationWarnings": "Enabled",
"C_Cpp.debugger.useBacktickCommandSubstitution": false,
"C_Cpp.default.enableConfigurationSquiggles": true,
"C_Cpp.default.systemIncludePath": null,
"C_Cpp.dimInactiveRegions": true,
"C_Cpp.enhancedColorization": "Enabled",
"C_Cpp.errorSquiggles": "Enabled",
"C_Cpp.exclusionPolicy": "checkFolders",
"C_Cpp.experimentalFeatures": "Disabled",
"C_Cpp.formatting": "clangFormat",
"C_Cpp.inactiveRegionBackgroundColor": "",
"C_Cpp.inactiveRegionForegroundColor": "",
"C_Cpp.inactiveRegionOpacity": 0.55,
"C_Cpp.intelliSenseCachePath": "",
"C_Cpp.intelliSenseCacheSize": 5120,
"C_Cpp.intelliSenseEngineFallback": "Enabled",
"C_Cpp.intelliSenseMemoryLimit": 4096,
"C_Cpp.intelliSenseEngine": "Default",
"C_Cpp.loggingLevel": "Debug",
"C_Cpp.preferredPathSeparator": "Forward Slash",
"C_Cpp.suggestSnippets": true,
"C_Cpp.vcpkg.enabled": true,
"C_Cpp.workspaceParsingPriority": "highest",
"C_Cpp.workspaceSymbols": "All",
"editor.linkedEditing": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.formatOnType": true,
"diffEditor.codeLens": true,
"diffEditor.ignoreTrimWhitespace": true,
"diffEditor.maxComputationTime": 5000,
"diffEditor.renderIndicators": true,
"diffEditor.renderSideBySide": true,
"files.autoSave": "afterDelay",
"files.autoGuessEncoding": true,
"files.autoSaveDelay": 1000,
"files.defaultLanguage": "",
"files.exclude": {},
"files.saveConflictResolution": "askUser",
"search.exclude": {},
"search.quickOpen.history.filterSortOrder": "default",
"search.searchEditor.defaultNumberOfContextLines": 1,
"search.searchEditor.doubleClickBehaviour": "goToLocation",
"search.searchEditor.reusePriorSearchConfiguration": true,
"search.seedOnFocus": true,
"search.seedWithNearestWord": false,
"editor.find.addExtraSpaceOnTop": true,
"editor.find.autoFindInSelection": "never",
"editor.find.cursorMoveOnType": true,
"editor.find.loop": true,
"editor.find.seedSearchStringFromSelection": true,
"task.saveBeforeRun": "always",
"debug.showInlineBreakpointCandidates": true,
"debug.inlineValues": true,
}
3 测试
3.1 添加main.c
根目录下添加 LeetCode 3 的c代码
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int lengthOfLongestSubstring(char * s){
unsigned short start=0, maxLength=0;
bool tag[128] = {0};
for(unsigned short end = 0; end < strlen(s); end++){
while (tag[s[end]]) {
tag[s[start]]=false;
start++;
}
tag[s[end]] = true;
maxLength = maxLength > end-start + 1?maxLength:end-start + 1;
}
return maxLength;
}
int main() {
char s[] = "bbbbb";
printf("%d", lengthOfLongestSubstring(s));
}
3.2 调试
F5调试,进入断点成功