vscode如何编译运行c/c++程序

vscode如何编译运行c/c++程序

c++b4f0734bfe6d1eb06384Microsoft YaHei”, “SF Pro Display”, Roboto, Noto, Arial, “PingFang SC”, sans-serif; color: rgb(79, 79, 79); line-height: 32px; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; white-space: normal; background-color: rgb(255, 255, 255);”>安装微软C/C++插件

在vs code的扩展图标里搜索c++,安装c/c++插件并重载vs code

vscode如何编译运行c/c++程序

重启VS Code之后,打开一个含c/c++源码的文件夹,VS Code将会创建一个名为.vscode的子文件夹用于存放配置文件。

安装MinGW

去官网下载安装器,安装好后选择需要安装的工具:

立即学习C++免费学习笔记(深入)”;

vscode如何编译运行c/c++程序

选择好之后应用更改:

vscode如何编译运行c/c++程序

然后修改系统的环境变量,将可执行文件目录加入到Path中:

vscode如何编译运行c/c++程序

智能提示

为实现代码补全,需要创建一个c_cpp_properties.json文件。新建一个hello.c,输入:

#include <stdio.h> int main(void) {     printf("hello");     getchar();     return 0; }</stdio.h>

VS Code会提示找不到头文件,点击错误处的小灯泡,编辑包含路径

vscode如何编译运行c/c++程序

在c_cpp_properties.json中修改Win32下的路径设置

"browse": {     "path": [         "${workspaceRoot}",         "C:DevProgramMinGWlibgccmingw326.3.0includec++"     ],     "limitSymbolsToIncludedHeaders": true,     "databaseFilename": "" }

返回编辑器,点击小灯泡,将头文件所在目录添加到包含路径中

vscode如何编译运行c/c++程序

构建程序

构建程序需要创建一个tasks.json文件,点击任务->配置任务:

vscode如何编译运行c/c++程序

在弹出的选项中选择Others,然后修改tasks.json

{     "version": "2.0.0",     "tasks": [         {             "taskName": "build hello",             "type": "shell",             "command": "g++",             "args": [                 "-g", "hello.c"             ],             "group": {                 "kind": "build",                 "isDefault": true             }         }     ] }

调试代码

调试代码需要创建一个launch.json文件,点击调试栏的齿轮图标

vscode如何编译运行c/c++程序

修改launch.json

{         "version": "0.2.0",         "configurations": [             {                 "name": "(gdb) Launch",                 "type": "cppdbg",                 "request": "launch",                 "program": "${workspaceFolder}/a.exe",                 "args": [],                 "stopAtEntry": false,                 "cwd": "${workspaceFolder}",                 "environment": [],                 "externalConsole": true,                 "MIMode": "gdb",                 "miDebuggerPath": "C:DevProgramMinGWbingdb.exe",                 "setupCommands": [                     {                         "description": "Enable pretty-printing for gdb",                         "text": "-enable-pretty-printing",                         "ignoreFailures": true                     }                 ],                 "preLaunchTask": "build hello"             }         ]     }

调试运行

返回编辑器,设置断点,按F5开始调试

vscode如何编译运行c/c++程序

相关文章教程推荐:vscode教程

以上就是#%#$#%@%@%$#%$#%#%#$%@_e2f

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享