vscode下的Arduino代码出现意外编译错误

Unexpected compilation errors experienced with Arduino code under vscode

本文关键字:意外 编译 错误 下的 Arduino 代码 vscode      更新时间:2023-10-16

我已经尝试让vscode的Arduino扩展可靠地工作了一段时间。产生问题的草图的精简版本如下:

// Object to store read CPPM values
struct CPPMFrame {
// Analog sticks (values -1000 to 1000)
int pitch = 0;
int roll = 0;
int thr = 0;
int yaw = 0;
// Switches/dials (values -1000 to 1000, usually -1000, 0, or 1000)
int aux1 = 0;
int aux2 = 0;
};
void readCPPM(CPPMFrame* frame) {
//...
}
void setup() {//...}
void loop() {//...}

当用F1+Arduino: Verify"验证"时,在Output窗口中产生以下内容:

[Starting] Verify sketch - Joystick.ino
[Warning] Output path is not specified. Unable to reuse previously compiled files. Verify could be slow. See README.
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
Joystick:14:15: error: variable or field 'readCPPM' declared void
void readCPPM(CPPMFrame* frame) {
^
Joystick:14:15: error: 'CPPMFrame' was not declared in this scope
Joystick:14:26: error: 'frame' was not declared in this scope
void readCPPM(CPPMFrame* frame) {
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterDebug-JoystickTest.ino: In function 'void setup()':
Debug-JoystickTest:170:6: error: redefinition of 'void setup()'
void setup() {
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterDebug-CPPM-Monitor.ino:37:6: note: 'void setup()' previously defined here
void setup()
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterDebug-JoystickTest.ino: In function 'void loop()':
Debug-JoystickTest:195:6: error: redefinition of 'void loop()'
void loop() {
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterDebug-CPPM-Monitor.ino:44:6: note: 'void loop()' previously defined here
void loop()
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterJoystick.ino: In function 'void setup()':
Joystick:18:6: error: redefinition of 'void setup()'
void setup() {}
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterDebug-CPPM-Monitor.ino:37:6: note: 'void setup()' previously defined here
void setup()
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterJoystick.ino: In function 'void loop()':
Joystick:20:6: error: redefinition of 'void loop()'
void loop() {}
^
c:UsersneilbDocumentsGitHubArduino-CPPM-AdapterDebug-CPPM-Monitor.ino:44:6: note: 'void loop()' previously defined here
void loop()
^
exit status 1
[Error] Exit with code=1

这些错误或警告都不是由Arduino IDE 1.8.7产生的,它在那里进行了完美的验证。我不确定它是否有用,但这是我的arduino.jsonc_cpp_properties.json

/vscode/arduino.json

{
"board": "arduino:avr:micro",
"sketch": "Joystick.ino"
}

/vscode/c_cpp_properties.json

{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\Program Files (x86)\Arduino\tools\**",
"C:\Program Files (x86)\Arduino\hardware\arduino\avr\**",
"C:\Users\neilb\Documents\Arduino\libraries\CPPM",
"C:\Users\neilb\Documents\Arduino\libraries\Joystick"
],
"forcedInclude": [
"C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Arduino.h"
],
"intelliSenseMode": "msvc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}

这里发生了什么?

您似乎有两个问题,一个是JoystickTest.ino和CPPM-Monitor.ino都是同时编译的,这个答案没有深入探讨,另一个是编译错误

Joystick:14:15: error: variable or field 'readCPPM' declared void
void readCPPM(CPPMFrame* frame) {
Joystick:14:15: error: 'CPPMFrame' was not declared in this scope
Joystick:14:26: error: 'frame' was not declared in this scope
void readCPPM(CPPMFrame* frame) {

这看起来像Arduino的特殊原型处理。通常在C和C++中,如果你试图在函数原型声明之前使用它,你会遇到编译器错误。为了使Arduino在编译时对新手更友好,它首先扫描源文件,使其仅,列出函数列表,以便您可以调用setup中的函数,即使该函数稍后在文件中定义。当函数只有intchar *等基本类型时,这种方法很好,但如果使用枚举、结构等,则会失败,因为编译器当时还没有解析这些类型。

短期补救措施是强制生成明确的原型,例如,你可以把一个放在前面:

void readCPPM(CPPMFrame* frame);
void readCPPM(CPPMFrame* frame) {
...

尽管从长远来看,最好不要将所有内容都塞进主ino文件,而是将这些代码放入具有相应头文件的单独源文件中,这样可以完全避免这个问题。