ubuntu vscode C/C++配置(单文件)
注意是单文件编译运行,多文件等后面遇到了再更
官方教程参考:https://code.visualstudio.com/docs/cpp/config-linux#_running-the-build
自动化脚本【懒得一步步配的可以试试,不保证一定成功】
https://files.cnblogs.com/files/blogs/771798/ubuntu_vscode_cpp_setup.sh?t=1770286887&download=true
使用方法:
cd 文件目录
sudo chmod+x ubuntu_vscode_cpp_setup.sh
./ubuntu_vscode_cpp_setup.sh
脚本执行完后跳到步骤2.3
1.安装软件、插件及工具
1.1 安装vscode
sudo snap install code --classic
1.2 vscode安装C/C++插件
code --install-extension ms-vscode.cpptools --force
1.3 安装基础工具
sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential gdb
2.运行C++代码进行测试
2.1 创建项目目录
mkdir ~/Desktop/projects
cd ~/Desktop/projects
mkdir helloworld
cd helloworld
code .
2.2 写入cpp文件
创建一个helloworld.cpp文件,将如下代码复制进去
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
return 0;
}

2.3 运行


出现以下结果则说明运行正常(第一次运行会需要等待一段时间)

2.4(可选)解决头文件报错问题

- 在vscode中按F1键,选择C/C++:Edit Cofigurations(UI)
![image]()
- 对比以下几个地方,改为相同
![image]()
![image]()




浙公网安备 33010602011771号