You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求指导Ubuntu下VSCode连接远程树莓派的编译调试配置方法

Hey Massimo, let's get your VS Code on Ubuntu set up to compile and debug remotely on your Raspberry Pi—since you already have GDB Server running on the Pi, we just need to tweak a few VS Code configurations to make this work smoothly. Here's a step-by-step breakdown tailored specifically for VS Code:

1. Install Required VS Code Extensions

First, grab the extensions that make remote development and C/C++ debugging possible:

  • Open the Extensions panel (click the square icon on the left sidebar, or press Ctrl+Shift+X).
  • Search for and install:
    • Remote - SSH: This core extension lets VS Code connect directly to your Pi over SSH.
    • C/C++ Extension Pack: Bundles all the tools needed for compiling, debugging, and syntax highlighting for C/C++ projects (perfect for your GDB Server workflow).
2. Set Up SSH Connection to Your Raspberry Pi

Now let's link VS Code to your Pi:

  • Press F1 to open the command palette, then type Remote-SSH: Connect to Host... and select it.
  • Choose Add New SSH Host, then enter your Pi's SSH command (e.g., pi@raspberrypi.local if you're on the same network, or pi@192.168.x.x using its static IP).
  • Save the host configuration to your default SSH config file (usually ~/.ssh/config).
  • Reopen the command palette, select Remote-SSH: Connect to Host... again, and pick the Pi host you just added. VS Code will launch a new window connected to your Pi—you’ll need to enter your Pi’s password (or set up SSH key authentication to skip this step every time).
3. Access Your Project on the Pi

In the remote VS Code window:

  • Go to File > Open Folder and navigate to your project directory on the Pi. If your project isn’t on the Pi yet, you can drag-and-drop files from your Ubuntu machine to the remote file explorer, or use scp to transfer them beforehand.
4. Configure Compilation Tasks (tasks.json)

We need to tell VS Code how to build your project on the Pi:

  • Press Ctrl+Shift+B to open the build menu, then select Create tasks.json file from template > Others.
  • Replace the default content with a configuration matching your project:
    • For a Makefile project:
      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "build",
                  "type": "shell",
                  "command": "make",
                  "args": [],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  },
                  "problemMatcher": "$gcc"
              }
          ]
      }
      
    • For a direct GCC compile (adjust filenames as needed):
      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "build",
                  "type": "shell",
                  "command": "gcc",
                  "args": [
                      "-g",
                      "main.c",
                      "-o",
                      "main"
                  ],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  },
                  "problemMatcher": "$gcc"
              }
          ]
      }
      
  • Note: The -g flag is critical—it generates debug information so GDB can work with your executable.
5. Configure Remote Debugging (launch.json)

Now set up VS Code to connect to your Pi’s GDB Server:

  • Press F5 to start debugging setup, then select C++ (GDB/LLDB) when prompted. This generates a launch.json file.
  • Replace the default content with this configuration (adjust paths and port to match your setup):
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Remote GDB Debug",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/main", // Path to your compiled executable on the Pi
                "args": [], // Add any command-line arguments your program needs
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb", // Path to GDB on your Ubuntu machine
                "miDebuggerServerAddress": "raspberrypi.local:2345", // Pi's address + GDB Server port
                "preLaunchTask": "build", // Automatically build before debugging
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for GDB",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
    
  • Important: On your Pi, start the GDB Server with a command like gdbserver :2345 ./main—make sure the port (2345 here) matches what’s in launch.json.
6. Test the Workflow
  • On your Pi, navigate to your project folder and start the GDB Server: cd /path/to/your/project && gdbserver :2345 ./main
  • Back in VS Code, press F5 to start debugging. You should be able to set breakpoints, step through code, inspect variables, and use all standard debugging features as if you’re working locally.
Troubleshooting Tips
  • If SSH fails to connect: Ensure SSH is enabled on your Pi with sudo systemctl enable ssh && sudo systemctl start ssh.
  • Version mismatches: Try to use matching GDB versions on your Ubuntu machine and Pi to avoid compatibility issues.
  • Port access: Make sure your Pi’s firewall allows incoming connections on the GDB Server port (e.g., 2345).
  • SSH key issues: If setting up key auth, ensure your Pi’s ~/.ssh/authorized_keys file has permissions set to 600 and the ~/.ssh folder is 700.

内容的提问来源于stack exchange,提问作者Massimo Veneziano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:56:52