VScode如何配置c/c++运行环境
vscode如何配置c/c++环境
下载
- Mingw
参考链接:https://blog.csdn.net/jiqiren_dasheng/article/details/103775488
笔者下载的x86_64-8.1.0-release-win32-sjlj离线包存放在百度网盘,需要的读者可以前往取用。
链接:https://pan.baidu.com/s/17hp8J_VDJwc9HjuocPBn4A
提取码:ulaj
安装
安装VS Code
下载好VS Code后,安装时可以自己选择安装路径,其他的添加在windows右键菜单创建
。
参见链接:https://www.php.cn/tool/vscode/450800.html
安装Mingw-w64
安装Mingw-w64时,在Architecture一栏如果32位就选i686,如果64位就选择x86_64,其他的默认就好。安装目录我自定在d盘
安装C/C++支持插件
打开VS Code在插件商店搜索C/C++这个插件进行安装。
配置
新建一个文件夹,然后,右键选择用VS Code打开,打开之后新建一个.c文件,进行环境配置。
配置 launch.json文件
点击左边活动栏的调试按钮,然后,点击配置按钮选择环境C++(GDB/LLDB);
之后在这个文件夹里会多出一个文件夹.vscode,这个文件夹里会生成一个json文件——launch.json,然后将下面的代码替换掉里面的代码;
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb)c_launch",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x64",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"miDebuggerPath": "D:/init_all/vscode_editor/Mingwgcc/mingw64/bin/gdb.exe",
"MIMode": "gdb",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"internalConsoleOptions": "openOnFirstSessionStart",
"externalConsole": true,
"preLaunchTask": "gcc"
}
]
}
其中,第12行的*targetArchitecture*
根据自己所需的构架来更改,第14行的*miDebuggerPath*
需要按照Mingw-w64的安装目录来更改,其他的默认就好;
配置 tasks.json文件
在状态栏上选择Terminal
,在下拉选项中选择configTure Tasks...
;然后选择使用模板创建 tasks.json 文件这个选项;
接着就是选择Others 运行任意外部命令的示例这个选项;参考链接
随之则会生成一个tasks.json的文件,然后将下面的代码替换掉里面的代码;
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "gcc",
"args": ["-Wall", "-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"],
"echoCommand": true,
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
调试测试
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
int c = 0;
c = a + b;
printf("%d + %d = %d", a, b, c);
return 0;
}
还存在着一个问题——头文件哪里会出现一条绿色的波浪线,并且没有自动补全的功能;
配置 c_cpp_properties.json文件
光标放在头文件哪里会出现一个黄色的小灯泡,点击之后选择Edit "includePath" setting,随之会多出一个json文件c_cpp_properties.json,然后找到与Windows相关的头文件路径配置代码,即在"name": "Win32"的下面。根据Mingw-w64的安装路径找到头文件的所在路径,分别放在includePath和path中,即是下面有注释的地方:
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
},
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Win32",
"includePath": [
"C:/mingw-w64/mingw64/include",
"C:/mingw-w64/mingw64/x86_64-w64-mingw32/include",
//根据Mingw-w64的安装路径更改
"${workspaceRoot}"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"C:/mingw-w64/mingw64/include",
"C:/mingw-w64/mingw64/x86_64-w64-mingw32/include",
//根据Mingw-w64的安装路径更改
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 3
}
现在没有了绿色波浪线,也可以实现自动补全了,所以配置完成。
使用的时候注意用打断点运行,先打断点,然后F5;这样才能看到黑窗口
以上内容参考链接有
主要配置和使用:https://www.jianshu.com/p/b7cc0e36cd5f
vscode右键项目文件夹打开方法:https://www.php.cn/tool/vscode/450800.html
MinGW-w64 C/C++编译器下载和安装
https://blog.csdn.net/jiqiren_dasheng/article/details/103775488
https://pan.baidu.com/s/17hp8J_VDJwc9HjuocPBn4A
写本文的目的是结合自身实践,有些地方有出入,我来分享我自己遇到的问题以及如何解决的。