请求指导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
F1to open the command palette, then typeRemote-SSH: Connect to Host...and select it. - Choose
Add New SSH Host, then enter your Pi's SSH command (e.g.,pi@raspberrypi.localif you're on the same network, orpi@192.168.x.xusing 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 Folderand 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 usescpto 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+Bto open the build menu, then selectCreate 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" } ] }
- For a Makefile project:
- Note: The
-gflag 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
F5to start debugging setup, then selectC++ (GDB/LLDB)when prompted. This generates alaunch.jsonfile. - 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 inlaunch.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
F5to 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_keysfile has permissions set to600and the~/.sshfolder is700.
内容的提问来源于stack exchange,提问作者Massimo Veneziano
相关产品推荐
相关产品推荐

