win10+vscode配置c++环境(纯小白傻瓜操作)
总体流程:
下载安装vscode
安装cpptools插件
安装MinGW
安装编译、调试环境
修改vscode调试配置文件
1.通过anaconda下载vscode,方便配置库
2.在vscode中安装cpptools插件
3.安装MinGW
http://mingw.org/ 进入官网点击下载安装器 mingw-get-setup.exe(找不到的话请搜索)
配置环境变量:把F:\MinGW\bin添加进系统path(路径按你自己的安装做更改)。
选中几个需要的项右键Make for Installation进行标记,其中gcc和g++为c和c++编译器。gdb为必选,否则编译会出现问题。
选择完全部想要安装的项后点击左上角Installation菜单下的Apply Changes应用修改,过程需联网,中间出现error可先继续。
4.配置文件
vscode调试需要在打开的文件夹中进行(注意!是用VSCode打开文件夹!!,单独打开一个单独的cpp是没有下面的几个配置文件出现的!!)
打开文件夹后,新建test.cpp进行输入代码测试
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "aaa";
for(int i = 0; i < s.length(); ++i) {
cout << s[i] << endl;
}
system("pause");
return 0;
}
F5进行调试,出现一下:
选择C++(GDB),再选择g++,会自动生成launch.json的启动配置文件。
用以下代码覆盖launch.json配置文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "F:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Ctrl+Shirft+P调出命令行界面,输入Tasks:Configure Task,选择创建新的,再选择others,系统会自动生成tasks.json文件,用以下代码覆盖:
{
"version": "0.1.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
}
}
}
在.vscoe文件夹里创建c_cpp_properties.json文件:
在终端输入gcc -v -E -x c++ -来获得"includepath"
根据终端输出来配置:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"F:/MinGW/include/**",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include/c++",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include/c++/mingw32",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include/c++/backward",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/../../../../include",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include-fixed"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"F:/MinGW/include/**",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include/c++",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include/c++/mingw32",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include/c++/backward",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/../../../../include",
"F:/MinGW/bin/../lib/gcc/mingw32/9.2.0/include-fixed"
]
}
}
],
"version": 4
}
另一个版本的c_cpp_properties.json文件:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "F:\\MinGW\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
一劳永逸:因为VS需要为每一个文件夹做单独配置,所以建议把.vscode文件夹放到你常用的文件夹的顶层,这样就不用重复配置了。
不用每个新cpp文件就要一套配置。这些配置在你配置好的文件夹内的所有子文件夹和文件都能使用。
最后运行test.cpp,成功!
其他可行版本(强烈推荐)
1.tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++", //这里注意一下,见下文
"command": "F:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-ggdb3", // 生成和调试有关的信息
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接
"-std=c++17", // 使用c++17标准
"-finput-charset=UTF-8", //输入编译器文本编码 默认为UTF-8
"-fexec-charset=GB18030", //输出exe文件的编码
"-D _USE_MATH_DEFINES"
],
"options": {
"cwd": "F:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"presentation": {
"echo": true,
"reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
"focus": false,
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
}
]
}
2.launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "F:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
3.c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW64",
"intelliSenseMode": "gcc-x64",
"compilerPath": "F:\\MinGW\\bin\\g++.exe",
"includePath": [
"${workspaceFolder}"
],
"cppStandard": "c++17"
}
],
"version": 4
}