首先記錄一下前面運行程序時出現的兩個小問題:
C語言中黑窗口一閃而過解決辦法
加入頭文件:
#include <stdlib.h>
system("pause");
使用vscode進行C++調試任務
調試程序時找不到g++任務:
需要修改launch.json中的preLaunchTask標簽與tasks.json中的label保持一致就行:
"label": "C/C++: g++.exe 生成活動文件",
"preLaunchTask": "C/C++: g++.exe 生成活動文件",
解決vscode運行代碼終端輸出中文亂碼的問題
方法1:
#include <Windows.h> //加入該頭文件
int main(){
SetConsoleOutputCP(CP_UTF8);
}
但是此方法需要在每個文件當中都添加才行,過于麻煩。
方法2:
在設置中搜索:encoding,
找到如下設置項:
Files: Encoding
在讀取和寫入文件時使用的默認字符集編碼。可以按語言對此項進行配置
選擇GBK,后重啟vscode
但此方法只對新建文件有效,
已存在的文件中原本的中文又會變為亂碼,
對于已存在的文件,可重新打開后把亂碼刪除,重新輸入中文,這也相對麻煩。
方法3:
tasks.json文件中加入"args"中加入參數:"-fexec-charset=GBK",
后重啟vscode
{
"version": "2.0.0",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK", //加入此參數解決中文亂碼問題
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"tasks": [
{
"type": "cppbuild",
"label": "gcc",
"command": "D:\\software\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "調試器生成的任務。"
}
]
}