目录
一、安装VSCode
二、安装MinGW编译器
1、配置环境变量
2、测试配置是否成功
三、配置VSCode
1、安装所需扩展
2、新建代码存放文件夹
3、添加配置文件
4、配置文件内容
(1)c_cpp_properties.json
(2)launch.json
(3)settings.json
(4)tasks.json
测试
其他:C++程序运行
每次换了电脑都忘记配环境的教程,这次直接做个笔记得了。
一、安装VSCode
VSCode安装教程(默认已经装好)
VSCode安装使用教程(最新详细版)_vscode安装教程-CSDN博客
VSCode只是编辑器,所以还需要一个编译器,这里使用MinGW编译器。
二、安装MinGW编译器
下载链接:MinGW Distro - nuwen.net
找到下载的安装包位置,双击,出现一个窗口,意思是选择安装路径,我安装在了D:\software\MinGW\中。然后点击“Extract”安装。
1、配置环境变量
安装完成后打开安装后的目录,打开bin文件目录,复制地址栏【我的路径是D:\software\MinGW\MinGW\bin】。
win11直接搜索“查看高级系统设置”。
查看环境变量
环境变量配置成功,测试是否配置成功。
2、测试配置是否成功
Win+R调出命令提示符,输入cmd,弹出命令行。输入g++ --version,如图显示版本表示配置成功。
三、配置VSCode
1、安装所需扩展
需要安装的:
C/C++ 【必备】Code Runner【必备】Rainbow Brackets【帮助阅读工具, 按需安装】Chinese(Simplified)【中文环境, 按需安装】2、新建代码存放文件夹
安装完成后新建文件夹存放文件(尽量路径中不要有中文),我的是G:\MyCode\目录。
然后在vscode中文件-->打开文件夹。
3、添加配置文件
点击新建文件夹,并将文件夹命名为.vscode(只能是这个名字)
点击新建文件,新建四个文件。文件名分别为:
c_cpp_properties.json
launch.json
settings.json
tasks.json
4、配置文件内容
(1)c_cpp_properties.json
{ "configurations": [ { "name": "Win64", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "D:/software/MinGW/MinGW/bin/g++.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 }
其中,compilerPath这一项要把路径改成刚才g++的安装路径(在MinGW编译器安装的bin目录下)
(2)launch.json
{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:/software/MinGW/MinGW/bin/gdb.exe", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
miDebuggerPath这一项也要把路径改成刚才g++的安装路径(在MinGW编译器安装的bin目录下)
(3)settings.json
{ "files.associations": { "*.py": "python", "iostream": "cpp", "*.tcc": "cpp", "string": "cpp", "unordered_map": "cpp", "vector": "cpp", "ostream": "cpp", "new": "cpp", "typeinfo": "cpp", "deque": "cpp", "initializer_list": "cpp", "iosfwd": "cpp", "fstream": "cpp", "sstream": "cpp", "map": "c", "stdio.h": "c", "algorithm": "cpp", "atomic": "cpp", "bit": "cpp", "cctype": "cpp", "clocale": "cpp", "cmath": "cpp", "compare": "cpp", "concepts": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", "cstdlib": "cpp", "cstring": "cpp", "ctime": "cpp", "cwchar": "cpp", "exception": "cpp", "ios": "cpp", "istream": "cpp", "iterator": "cpp", "limits": "cpp", "memory": "cpp", "random": "cpp", "set": "cpp", "stack": "cpp", "stdexcept": "cpp", "streambuf": "cpp", "system_error": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "xfacet": "cpp", "xiosbase": "cpp", "xlocale": "cpp", "xlocinfo": "cpp", "xlocnum": "cpp", "xmemory": "cpp", "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", "xtree": "cpp", "xutility": "cpp", "stdlib.h": "c", "string.h": "c" }, "editor.suggest.snippetsPreventQuickSuggestions": false, "aiXcoder.showTrayIcon": true}
(4)tasks.json
{ "version": "2.0.0", "tasks": [ { "label": "g++", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${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 } }, "group": { "kind": "build", "isDefault": true } } ]}
此时环境已经配好了。
注意:以后的C/C++代码文件必须放在.vscode文件夹的文件夹里,否则会出现报错。
测试
测试用了一个最简单的"Hello, World!" 程序。
#include <stdio.h>int main() { printf("Hello, World!\n"); return 0;}
以上就是所有的配置过程,比较简单,但是其中有部分比较细小但需要注意的事项。
其他:C++程序运行
linux和windows系统下,C++程序运行略有差距,这部分主要介绍的是windows系统、
创建一个test.cpp文件
#include <iostream>int main() { std::cout << "Hello, Merry!" << std::endl; return 0;}
新建终端
定位在代码所在目录中
编译
g++ test.cpp -o test
执行
./test