VSCode远程连接Ubuntu服务器进行c++开发
时间: 2025-06-11 18:32:16 浏览: 25
### VSCode 远程连接 Ubuntu 服务器进行 C++ 开发配置教程
#### ### 1. 软件安装步骤
在 Windows 平台上使用 VSCode 远程连接 Ubuntu 进行 C++ 开发,需要完成以下软件的安装和配置:
- **Linux 系统安装**:确保目标开发环境已安装 Ubuntu 系统,并完成基础配置[^1]。
- **VSCode 安装**:在 Windows 系统中下载并安装 Visual Studio Code。可以从官方站点获取最新版本的安装包[^2]。
- **SSH 安装**:在 Ubuntu 中安装 SSH 服务以支持远程连接。通过以下命令完成 SSH 服务的安装和启动:
```bash
sudo apt-get install openssh-server
sudo ufw allow ssh
sudo systemctl start ssh
```
此外,可以通过 `hostname -I` 获取 Ubuntu 的 IP 地址,用于后续连接配置[^4]。
#### ### 2. 配置 Remote-SSH 插件
在 VSCode 中安装并配置 Remote-SSH 插件,具体操作如下:
- 打开 VSCode 的扩展市场,搜索并安装 `Remote - SSH` 插件。
- 在 VSCode 的左侧边栏中点击 `Remote Explorer` 图标,选择 `Connect to Host`,然后输入目标 Ubuntu 服务器的连接信息,格式为 `User@HostName` 或 `User@IPAddress`[^3]。
- 如果是首次连接,VSCode 会提示输入密码并生成 SSH 配置文件 `.ssh/config`。配置文件内容示例如下:
```plaintext
Host Ubuntu
HostName 192.168.1.100
User ubuntu_user
```
#### ### 3. 安装远程插件
在成功连接到 Ubuntu 服务器后,VSCode 会自动切换到远程模式。此时需要安装以下插件以支持 C++ 开发:
- **C/C++ 插件**:提供 IntelliSense、代码导航、调试等功能。
- **CMake Tools**(可选):如果项目依赖 CMake 构建系统,则需要安装此插件。
- **Code Runner**(可选):快速运行代码片段。
#### ### 4. 配置 VSCode 开发环境
为了实现高效的 C++ 开发,需要正确配置以下文件:
- **c_cpp_properties.json**:定义编译器路径和包含路径,确保 IntelliSense 正常工作。示例配置如下:
```json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/c++/9"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
```
- **tasks.json**:定义任务以执行构建操作。示例配置如下:
```json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
```
- **launch.json**:配置调试器以支持断点调试。示例配置如下:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
```
#### ### 5. 简单小测试
完成上述配置后,可以创建一个简单的 C++ 程序进行测试。例如,在远程环境中新建 `test.cpp` 文件,内容如下:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, Remote World!" << std::endl;
return 0;
}
```
保存文件后,按下 `Ctrl+Shift+B` 触发构建任务,随后使用调试功能运行程序。如果一切正常,终端将输出 `Hello, Remote World!`[^2]。
---
阅读全文
相关推荐




















