配置c++环境
前言
本人环境:Windows10 64位
VSCode 使用起来还是很方便的,运行速度快,占用系统资源小,还有丰富的插件
软件安装
1.下载安装MinGW-w64及环境变量配置
MinGW:可以理解成包含多种编译环境的可自由选择安装的其中的部分的软件
下载地址:https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/
直接弹窗下载即可,下载完按照下面教程安装配置即可。
下载相关文件(重要步骤)
打开MinGW,勾选编译器软件
- mingw32-gcc.bin(c语言文件编译器)
- mingw32-gcc-g++.bin(c++语言编译器)
- mingw32-gdb.bin(调试编译后文件)
有空间的话,基本包建议全部安装
若在安装的时候报“找不到 xxx 库”的错误,可以重新勾选此库和dll文件,多次重复下载即,可选择完后,点击 Installation > applychange,等待安装完就ok
添加环境变量
在系统变量中选择 Path,新建,将 bin地址 加入环境变量 (我的是 “C:\MinGW\bin”)
检测是否安装成功:命令提示符中输入 gcc -v ,若显示版本号则说明安装成功
配置文件
在此文件夹新建一个 .vscode 文件夹
在 .vscode 文件夹中新建三个文件
- c_cpp_properties.json
- launch.json
- tasks.json
将下列代码复制到文件中
c_cpp_properties.json
“includePath” 路径获取方法 cmd 下输入: gcc -v -E -x c++ -
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include-fixed
修改 “includePath” 和 "Path"
“c:/mingw/include/**” 此条路径也根据你的路径修改加入即可
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"c:/mingw/include/**",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include/c++",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include/c++/mingw32",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include/c++/backward",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../include",
"c:/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}",
"c:/mingw/include/**",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include/c++",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include/c++/mingw32",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include/c++/backward",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../include",
"c:/mingw/bin/../lib/gcc/mingw32/9.2.0/include-fixed"
]
}
}
],
"version": 4
}
launch.json
根据自己路径修改 "miDebuggerPath"
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Main",
"request": "launch",
"mainClass": "Main",
"projectName": "C++_Code_142e2023"
},
{
"type": "java",
"name": "Launch HelloWorld",
"request": "launch",
"mainClass": "HelloWorld",
"projectName": "C++_Code_142e2023"
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/exe/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "c:/MinGW/bin/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
直接复制粘贴即可,无需修改
这里我顺便把 exe 的路径改了。在 “args” 中,需要进行修改可自行百度,此处我的会将编译生成的 exe 文件单独放在一个 exe文件夹 中
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"-fexec-charset=GBK", // 处理mingw中文编码问题
"-finput-charset=UTF-8",// 处理mingw中文编码问题
"${file}",
"-o",
"${workspaceFolder}/exe/${fileBasenameNoExtension}.exe"
], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"\\"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
这里解释一下args中自定义 exe文件存放路径:
${workspaceFolder}: 当前工作路径
exe: 自己建的文件夹
${fileBasenameNoExtension}: 当前文件的文件名,NoExtension意思就是不带后缀名,然后加个 .exe 就是当前的exe文件,也就是编译生成的 exe 文件
上述路径放一起意思就编译生成的 exe 文件放在exe文件夹中, 把这条配置删除的话,编译生成的exe文件就默认放在了当前工作路径下。
顺便说一下 vscode 中一些变量代表什么意思
- ${workspaceFolder} :表示当前workspace文件夹路径,也即/home/Coding/Test
- ${workspaceRootFolderName}:表示workspace的文件夹名,也即Test
- ${file}:文件自身的绝对路径,也即/home/Coding/Test/.vscode/tasks.json
- ${relativeFile}:文件在workspace中的路径,也即.vscode/tasks.json
- ${fileBasenameNoExtension}:当前文件的文件名,不带后缀,也即tasks
- ${fileBasename}:当前文件的文件名,tasks.json
- ${fileDirname}:文件所在的文件夹路径,也即/home/Coding/Test/.vscode
- ${fileExtname}:当前文件的后缀,也即.json
- ${lineNumber}:当前文件光标所在的行号
- ${env:PATH}:系统中的环境变量
0 条评论