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

Ubuntu下树莓派400的VSCode调试GPIO权限问题咨询

树莓派400 Ubuntu环境下VSCode调试wiringPi权限问题解决

问题背景

  • 目标:在Ubuntu系统上为树莓派400配置VSCode调试环境
  • 现状:编译后的程序需通过sudo ./program_name运行,直接执行会提示权限拒绝;不想以root权限启动VSCode
  • 调试报错:启动调试(按F5)时,因wiringPi无法访问GPIO硬件,触发权限拒绝错误:
wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: Permission denied.
  Aborting your program because if it can not access the GPIO
  hardware then it most certianly won't work
  Try running with sudo?
[1] + Done                       "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-j40gmjsr.mas" 1>"/tmp/Microsoft-MIEngine-Out-vltmyj1b.a3g"
  • 当前launch.json配置:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

解决方法

方法一:修改launch.json让调试程序以sudo运行

无需以root启动VSCode,只需配置调试器通过sudo启动目标程序。修改launch.json的对应配置项:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "/usr/bin/sudo",
            "args": ["${fileDirname}/${fileBasenameNoExtension}"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

注意:首次运行调试时,VSCode调试控制台可能不会弹出sudo密码输入框,建议先在终端执行一次sudo ls并输入密码,让系统保留sudo会话,再启动调试。

方法二:给当前用户添加GPIO权限(推荐)

通过将用户加入gpio用户组,永久获取GPIO访问权限,无需每次使用sudo:

  1. 执行终端命令:
sudo usermod -aG gpio $USER
  1. 注销当前用户并重新登录(或重启系统),使组权限生效。
  2. 之后直接运行程序或启动调试,无需sudo,VSCode保持普通用户运行即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 03:05:24