一、配置C/C++环境
1、下载MinGWX64.zip,解压后放到指定位置,如:C:\Program Files
2、配置环境变量:C:\Program Files\mingw64\bin
3、在cmd窗口中输入:gcc -v ,出现以下内容表示配置成功
二、配置VScode
1、在磁盘上创建工作区,也就是创建文件夹并命名。本人已在E盘创建文件夹cppStudy
2、创建.vscode文件夹
3、在.vscode文件夹中创建3个json文件,分别命名为:launch.json tasks.json 、c_cpp_properties.json
3.1、launch.json内容如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw64\bin\\gdb.exe", /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++"
}
]
}
3.2、tasks.json内容如下:
{
"version": "2.0.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
}
}
}
3.3、c_cpp_properties.json内容如下:
{
"configurations": [
{
"name": "Win32",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/mingw64/include",
"C:/Program Files/mingw64/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__=7",
"__cdecl=__attribute__((__cdecl__))"
],
"compilerPath": "",
"browse": {
"path": [
"${workspaceFolder}",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/mingw64/include",
"C:/Program Files/mingw64/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"
]
},
"cppStandard": "c++17",
"cStandard": "c17"
}
],
"version": 4
}
三、测试环境:
1、编写C语言程序
2、编写C++程序
四、注意的问题
1、关于配置文件:如果在VScode中新建文件夹保存C/C++程序需要重新配置文件夹.vscode以及三个配置文件,所以建议在最外层的文件夹中创建配置文件夹.vscode及配置文件,在子文件夹中保存C/C++程序
2、关于C/C++输出的中文乱码:在vscode的setting.json文件中做如下配置
3、查看gcc编译器版本号和安装路径的指令:gcc -v -E -x c++ -