ubuntu基于VSCode的C++编程语言的构建调试环境搭建指南
首先安装g++
sudo apt install g++
- 检查是否安装成功:
在插件栏安装插件c/c++、code runner:
首先写一个简单的demo.cpp文件:
'''
#include<iostream> using namespace std; int main() { int i = 0 ; int num =0; for ( ; i < 10; i++) { num += i; } cout<<"结果是"<<num<<endl; return 0; }
'''
生成配置文件tasks.json:
- 终端 -> 默认配置生成任务
- ctrl + shift + p 在出现的命令行内输入c/c++
'''
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++ build active file", "command": "/usr/bin/g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "/usr/bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] }
'''
生成launch.json配置文件:
ctrl + shift + p 命令行输入:Debug
'''
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++ build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "g++ build active file", "miDebuggerPath": "/usr/bin/gdb" } ] }
'''
运行demo.cpp:鼠标右击选择运行或右上角三角运行符号
进行调试:
来源:https://www.cnblogs.com/lhx9527/p/12596281.html