如果你想把一段代码运行起来,就需要用到编译器和编辑器,计算机只认二进制,想让机器识别我们所写的代码就需要编译器,编译器是必不可少的
编译器:就是把我们写的代码 编译成目标二进制代码
编辑器:是写代码的,就如文本编辑器txt这种,本文用的是vs code编辑器,写代码专用的编辑器更加方便
vscode网页下载地址https://code.visualstudio.com/
一,安装C/C++编译器
通过minGW64安装
通过我分享的百度网盘链接下载:https://pan.baidu.com/s/1Rovh6nIuW2wvcsLLRQa9dw?pwd=8899
提取码:8899
网址下载 https://sourceforge.net/projects/mingw-w64/
下载好之后把文件解压出来
下载路径配置
下载好之后,记得选一个你想放的文件夹地址,我这边放的地址是C:\Program Files\mingw64
推荐和我放一样的地址 后面好操作
环境变量配置
打开编辑环境变量
打开方式一:电脑桌面上一般都会有此电脑,右键此电脑 ---- 属性 ---- 高级系统设置 ---- 高级 ---- 环境变量
打开方式二:直接在电脑左下角有个搜索图标,点击搜索环境变量即可打开,我这里用的是win11,大致地方都一样的
打开之后进入这个界面
image.png
到环境变量之后,在系统变量里面找到这个path 点击编辑
环境变量
进入这个编辑环境之后,我们直接点击添加,然后把我们下的那个mingw64的文件路径复制一下,添加到里面点击确定就设置好环境了
image.png
我的文件路径是C:\Program Files\mingw64\bin
添加
测试一下是否配置好环境
W + R 输入cmd 回车 打开命令窗口 输入 gcc -v
配置成功画面
二,设置vs code编辑器
打开vscode 点击左边第五个按钮
打开扩展
搜索C/C++ 安装
image.png
如果说你的这个vscode是英文的,可以直接搜索Chinese中文简体,安装一下重新启动vscode之后编辑器就是中文的了
image.png
所有操作做完之后
三,创建文件夹文件基本操作
打开vscode 创建
image.png
创建好之后分别在.vscode下的三个文件添加以下代码
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/Program Files/mingw64/include/**",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"C:/Program Files/mingw64/include/**",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
}
}
],
"version": 4
}
!注意:代码中像这些C:\ 开头的,就是前面下的那个mingw64文件路径
如果说你是把mingw64 下在其他文件路径,就需要把代码中的那些路径替换到你的相应路径
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"preLaunchTask": "echo",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"echo.",
"&",
"pause"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole":true
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb
"preLaunchTask": "echo",//这里和task.json的label相对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"//解决中文乱码
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
弄好了之后就可以
image.png
写代码运行了
点击运行
运行成功
vscode环境配置就到此为止了,可以没事研究一下各个按钮的用处
网友评论