之前一直用 Visual Studio 2013 写 C++。 后面电脑重装之后,就没有环境了。 这次换成 Visual Studio Code + MinGW的模式试试。
安装 Visual Studio Code
前往官网下载安装包,然后一路 next 安装就行。
安装编译器:MinGW
在线安装
MinGW官网进行下载安装。点击下载,选择好安装路径(如C:)。
如果你网速不好的话,那你就需要看我的这一篇文章。
离线安装
链接: https://pan.baidu.com/s/1278b39kz585Q9Fw27IYbGw 提取码: idda
解压之后放置到安装目录(如C:)即可。
配置环境变量
鼠标右键“我的电脑”->“属性”,选择“高级”选项卡下的“环境变量”,在系统变量里点“新建”,之后填写MinGW的安装路径,如下:
| 变量名:MinGW 变量值:D:\Environment\MinGW
|
之后找到Path,在最前面添加下面这段声明%MinGW%\bin
,之后点击确定。
检查是否安装成功
| C:\Users\Tommy>gcc -v Using built-in specs. Target: i686-w64-mingw32 Configured with: ../gcc44-svn/configure --target=i686-w64-mingw32 --host=i686-w64-mingw32 --disable-multilib --disable-nls --disable-win32-registry --prefix=/mingw32 --with-gmp=/mingw32 --with-mpfr=/mingw32 --enable-languages=c,c++ Thread model: win32 gcc version 4.4.3 (GCC)
C:\Users\Tommy>g++ -v Using built-in specs. Target: i686-w64-mingw32 Configured with: ../gcc44-svn/configure --target=i686-w64-mingw32 --host=i686-w64-mingw32 --disable-multilib --disable-nls --disable-win32-registry --prefix=/mingw32 --with-gmp=/mingw32 --with-mpfr=/mingw32 --enable-languages=c,c++ Thread model: win32 gcc version 4.4.3 (GCC)
|
安装C++编辑器插件
在vscode的插件搜索输入框输入“C++”,搜索插件“C/C++ for Visual Studio Code”。点击install安装插件。
工作空间配置文件
建立一个空文件夹,新建 .vscode
文件。
文件下建立三个配置文件,文件名和内容分别为下面三个。
launch.json
注意 修改miDebuggerPath为你自己的路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\MinWG\\bin\\gdb.exe", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
|
tasks.json
注意修改你的command和cwd为你自己的路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++", "command": "C:\\MinWG\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-ggdb3", "-Wall", "-static-libgcc", "-std=c++17", "-finput-charset=UTF-8", "-fexec-charset=GB18030", "-D _USE_MATH_DEFINES" ], "options": { "cwd": "C:\\MinWG\\bin" }, "problemMatcher": [ "$gcc" ], "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, } ] }
|
| { "configurations": [ { "name": "MinGW64", "intelliSenseMode": "gcc-x64", "compilerPath": "C:\\MinWG\\bin\\g++.exe", "includePath": [ "${workspaceFolder}" ], "cppStandard": "c++17" } ], "version": 4 }
|
Hello World
在 .vscode
文件同层目录下,新建一个 test.cpp
,然后F5调试运行即可。
| #include<iostream> using namespace std; int main(){ cout << "Hello World" << endl; system("pause"); }
|