软件安装
- 这里略过VScode和MinGW-W64 GCC-8.1.0的安装过程,只说一下注意事项
安装注意
图中另有大哥做出了解释,经过几次尝试,最终配置成功的版本是上图第二个(不过区别不大) Visual Studio Code官网下载路径 MinGW-W64 GCC-8.1.0官网下载路径 MinGW-W64 GCC-8.1.0的安装路径需要记清楚,后续配置要用到
配置系统环境变量
win+q 系统环境变量中,添加环境变量path(MinGW-W64 GCC-8.1.0的安装路径)
- 我的路径是:
D:\vscode\MyVsTool\CEdit\MinGWIns\mingw64\bin
检查是否配置成功
- cmd中输入
gcc -v
VScode配置C/C++环境
- 下载C/C++插件
- 创建一个要用来运行C代码的文件夹,命名为
.vscode (配置相关文件,不要省略. ) - 在
.vscode 文件夹下创建三个文件c_cpp_properties.json 、launch.json 、tasks.json
c_cpp_properties.json 路径需要修改
{
"configurations": [
{
"name": "Win32",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\x86_64-w64-mingw32",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\backward",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\include",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include-fixed",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\bin\\..\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\..\\..\\..\\..\\x86_64-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=7",
"__cdecl=__attribute__((__cdecl__))"
],
"compilerPath": "",
"browse": {
"path": [
"${workspaceFolder}",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\x86_64-w64-mingw32",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\backward",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\include",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include-fixed",
"D:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\bin\\..\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\..\\..\\..\\..\\x86_64-w64-mingw32\\include"
]
},
"cppStandard": "c++17",
"cStandard": "c17"
}
],
"version": 4
}
{
"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:\\vscode\\MyVsTool\\CEdit\\MinGWIns\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++"
}
]
}
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
这里贴上我的测试代码
#include <stdio.h>
main()
{
printf("Hello World!\n");
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 100
#define ElemType int
#define Status int
typedef struct
{
ElemType *data;
int length;
}SqList;
void InitList(SqList &L)
{
L.data = new ElemType[MAXSIZE];
L.length = 0;
}
void PrintList(SqList L)
{
printf("当前顺序表的所有元素:");
for (int i = 0; i <L.length; i++)
{
printf("%d ", L.data[i]);
}
printf("\n");
}
void Create(SqList &L)
{
int n,i;
printf("请输入要输入元素的个数:");
scanf("%d", &n);
if (n<0 || n>MAXSIZE)
{
printf("请输入正确的个数!\n");
}
for (i = 0; i < n; i++)
{
printf("请输入第%d个元素:",(i+1));
scanf("%d", &(L.data[i]));
L.length++;
}
}
void MergeList(SqList &A, SqList &B, SqList &C)
{
int i=0, j=0,k=0;
int m = A.length;
int n = B.length;
while(j < n && i< m)
{
if (A.data[i]>B.data[j])
{
C.data[k] = B.data[j];
C.length++;
k++;
j++;
}
else
{
C.data[k] = A.data[i];
C.length++;
k++;
i++;
}
}
while (i < m)
{
C.data[k] = A.data[i];
C.length++;
k++;
i++;
}
while (j < n)
{
C.data[k] = B.data[j];
C.length++;
k++;
j++;
}
}
int main()
{
SqList A, B,C;
InitList(A);
InitList(B);
InitList(C);
printf("请输入集合A的元素:\n");
Create(A);
printf("请输入集合B的元素:\n");
Create(B);
MergeList(A, B, C);
PrintList(C);
}
这里附上过程中出现的问题
一、找不到任务“g++.exe build active file”
解决:preLaunchTask中的字段"preLaunchTask": “g++.exe build active file"未修改为"preLaunchTask”: “g++”,编译器无法识别;
二、控制台中文乱码
- 首先c语言的运行程序是调用的
cmd.exe 而window的cmd的编码默认为936也就是gb2312 。 - 运行->cmd->在白色窗口头部点击右键->属性 可以查看到,主要现在用utf-8较多,所以用utf-8编码的c程序出现了乱码
解决
在vs code修改代码的打开和保存方式
参考
VS Code 配置编译报错问题汇总 vs code解决C/C++控制台中文乱码
|