C++编译环境(MinGW)的安装
1. MinGW
目标:本地安装编译器MinGW,并设置系统路径。
相关资料:
MinGW官网链接
安装教程:MinGW安装教程——著名C/C++编译器GCC的Windows版本
自己的安装思路(图片均来自上面的安装教程):
第一步:从官网下载安装管理器。
第二步:在安装管理器中选择需要下载的部分。
第三步:将MinGW的bin文件夹添加到系统路径。
第四步:验证,在cmd中尝试使用gcc -v命令。
VS Code 插件安装
VSC的安装基本步骤:
第一步:从VS Code官网下载并安装VSC。
第二步:安装相关插件。
2.1. C/C++
作用:C/C++开发环境。
官方出品的 cpptools。
参考资料:
官网文档中有描述如何使用该插件。
2.2. Chinese (Simplified) Language Pack for Visual Studio Code
作用:中文翻译。
所有其他语言支持都是通过插件的形式完成。
2.3. Code Runner
作用:简单的运行程序(最新版本其实也没那么有用了)。
为什么要使用Code Runner:没有非常方便的编译并运行程序的功能。
其实最新版本有了,可以通过 C/C++: build and Debug Active File 实现。 来执行,即在tasks.json中配置label为g++.exe build active file的task。
使用方式:
通过右键 -> Run Code运行程序。
通过下图中的小图标。
可以在F1中运行命令。
原理(不确定):查了下,应该是根据code-runner.executorMap配置执行命令。
以c++为例,就是cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
其他设置:
Run In Terminal:勾选后,每次调用Run Code都可以。
Save All Files Before Run.
2.4. Settings Sync
作用:通过Github Gist同步VSC的配置。
使用:
和以前有些不同。点击上传配置后出现如下界面。
这时我们应该点击 login with github,然后在上传的时候选择我们所需的gist,如下图:
2.5. background
可以在配置中设置本地图片以及css样式。
举例:
需要注意的是本地文件格式。
customImages也可以通过互联网获取。
"background.useDefault": false,
"background.customImages": [
"file:///E:/photo/Fubuki/0be4612442a7d933fff9e281a34bd11372f00141.jpg"
],
"background.useFront": false,
"background.style": {
"content": "''",
"pointer-events": "none",
"position": "absolute",
"z-index": "99999",
"right": "200px",
"width": "100%",
"height": "100%",
"background-position": "0% 0%",
"background-size": "cover",
"background-repeat": "no-repeat",
"opacity": 0.1,
"object-fit": "scale-down",
},
![](https://rjwj-1252147446.file.myqcloud.com/2019/10/image_1df85artbrbq1jnljuh1s9c1fms9.jpg)
3. 编写并运行C++程序
3.1. 综述
参考资料:
官方文档:Using Mingw in Visual Studio Code
官方文档:IntelliSense
VSC中主要通过三个配置文件来对C++程序的编写、运行、调试进行配置:
c_cpp_properties.json:主要作用是配置IntelliSense。
launch.json:主要作用是帮助调试。
tasks.json:主要作用是构建应用程序。
对于给定下面的 hello world 代码进行测试(代码来自官方文档)。
#include #include #include using namespace std; int main() { vector msg {"Hello", "C++", "World", "from", "VS Code!"}; for (const string& word : msg) { cout Others 来创建。
tasks.json举例:
使用这个执行的就是g++ -g -o helloworld helloworld.cpp命令。
group中的内容,应该就是指的是通过 Tasks: Run Build Task 执行,即ctrl+shift+b。
在这个官方文档中有少量下面参数的介绍。
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"${fileBasenameNoExtension}",
"${fileBasenameNoExtension}.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
运行:
当拥有group为test的配置时,可以通过 Tasks: Run Test Task来运行程序。
当拥有group为build的配置时,可以通过 Tasks: Run Build Task来运行程序。
3.4. 调试程序
参考资料:cpptools文档 – Configuring launch.json for C/C++ debugging
如果要调试,就需要建立launch.json。
如果新建launch.json的方法:
直接创建文件。
通过下图新建。但好像其实选哪个都会新建launch.json文件。
MinGW GDB调试的launch.json举例:
注意preLaunchTask,这个名字对应的是 tasks.json 中的 label。
如果不设置preLaunchTask,则需要在调试前自己构建项目(即创建*.exe文件)。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "E:\\Software\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
调试界面:
通过F5进行调试。
转载需保留链接来源:软件玩家 » Win10中VS Code配置及C++ 环境的安装
|