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

VSCode Arduino扩展编译报错:'USB_VID'未在此作用域中声明

VSCode Arduino扩展编译报错:'USB_VID'/'USB_PID'未定义

我使用VSCode的Arduino扩展开发程序,每次验证或上传代码时都会触发USB_VID和USB_PID未定义的编译错误,但VSCode串口监视器能正常工作,且Arduino IDE可以正常编译上传。手动在代码中定义这两个宏能临时解决问题,但希望找到根本原因。


错误日志

Starting] Uploading sketch 'test.ino'
Please see the build logs in output path: /home/Documents/ETH/Semester 3/Innovations Project/Software/build
In file included from /home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBAPI.h:44:0,
                 from /home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.cpp:20:
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.cpp:75:29: error: 'USB_VID' was not declared in this scope
  D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
                             ^
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.h:270:61: note: in definition of macro 'D_DEVICE'
  { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
                                                             ^~~~
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.cpp:75:29: note: suggested alternative: 'USB_Recv'
  D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
                             ^
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.h:270:61: note: in definition of macro 'D_DEVICE'
  { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
                                                             ^~~~
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.cpp:75:37: error: 'USB_PID' was not declared in this scope
  D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
                                     ^
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.h:270:66: note: in definition of macro 'D_DEVICE'
  { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
                                                                  ^~~~
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.cpp:75:37: note: suggested alternative: 'USB_Recv'
  D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
                                     ^
/home/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.h:270:66: note: in definition of macro 'D_DEVICE'
  { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
                                                                  ^~~~

Error during build: exit status 1
IntelliSense configuration already up to date. To manually rebuild your IntelliSense configuration run \"Ctrl+Alt+I\"
[Error] Uploading sketch 'test.ino': Exit with code=1

测试代码(test.ino)

#if __has_include(<Arduino.h>)
  #include <Arduino.h>
const int ledPin = 15;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(100);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  Serial.println(ledPin);
  delay(100);                      // wait for a second
}

#endif

相关配置文件

arduino.json

{
    "board": "arduino:avr:micro",
    "programmer": "avrisp",
    "port": "/dev/ttyACM0",
    "output": "../build",
    "sketch": "test.ino",
    "intelliSenseGen": "global",
    "buildPreferences": [
        [
            "build.extra_flags",
            "-DMY_DEFINE=666 -DANOTHER_DEFINE=3.14 -Wall -std=c++14"
        ],
        [
            "compiler.cpp.extra_flags",
            "-DYET_ANOTER=\"hello\""
        ]
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: avr-g++ build active file",
            "command": "/home/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-w",
                "-std=c++14",
                "-fpermissive",
                "-fno-exceptions",
                "-ffunction-sections",
                "-fdata-sections",
                "-fno-threadsafe-statics",
                "-Wno-error=narrowing"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /home/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++"
        }
    ]
}

settings.json

{
    "C_Cpp.default.compilerPath": "/home/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++",
    "arduino.useArduinoCli": true,
    "arduino.enableUSBDetection": true
}

解决方案

1. 移除自定义编译任务

当前tasks.json中直接调用avr-g++编译单个文件的任务会绕过Arduino扩展的完整编译流程,导致板级预定义宏(USB_VID/USB_PID)无法被注入。删除该任务,改用VSCode命令面板中Arduino扩展提供的Arduino: Verify或Arduino: Upload命令执行编译。

2. 调整编译参数

arduino.json中build.extra_flags添加的-std=c++14可能与Arduino核心默认标准冲突,移除该参数,让扩展使用板卡默认编译标准。

3. 重置IntelliSense配置

按Ctrl+Alt+I手动重建IntelliSense配置;若仍有问题,删除.vscode文件夹下的c_cpp_properties.json,重新打开项目让扩展自动生成配置。

4. 验证Arduino CLI配置

在终端执行arduino-cli board details arduino:avr:micro,检查输出中是否包含USB_VID和USB_PID的定义,确认CLI能正确加载板卡配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 19:05:55