vscode下的Arduino代码出现意外编译错误
Unexpected compilation errors experienced with Arduino code under vscode
我已经尝试让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.json
和c_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
中的函数,即使该函数稍后在文件中定义。当函数只有int
、char *
等基本类型时,这种方法很好,但如果使用枚举、结构等,则会失败,因为编译器当时还没有解析这些类型。
短期补救措施是强制生成明确的原型,例如,你可以把一个放在前面:
void readCPPM(CPPMFrame* frame);
void readCPPM(CPPMFrame* frame) {
...
尽管从长远来看,最好不要将所有内容都塞进主ino文件,而是将这些代码放入具有相应头文件的单独源文件中,这样可以完全避免这个问题。
相关文章:
- 二叉排序树无法编译
- 编译时未启用intel oneApi CUDA支持
- vscode下的Arduino代码出现意外编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- 函数调用中出现意外编译错误 (C++)
- C++中出现意外的编译错误:将默认值传递给函数参数
- 错误 : 尚无法编译此意外的强制转换值
- 已编译的 protobuf 文件的意外输出路径
- 无法编译顶点着色器:令牌"<EOF>"出现意外$end
- 传递引用匿名实例时出现意外编译错误
- 在我的Mac OSX El Capitan上编译带有意外错误的简单C++类
- 获取0:1(1):错误:语法错误,尝试编译着色器时意外$end
- 意外的模板实例化导致编译错误
- 收到有关压缩结构的意外编译警告
- #include在查找预编译头文件时跳过——查找预编译头文件时意外结束
- 使用clang优化编译时得到意外结果
- 代码意外编译失败.为什么
- 在Visual Studio 2012中,作用域操作符中的错别字会导致意外的编译和递归
- 在visual studio中编译意外的(未声明的标识符)错误
- 编译 moc 文件 Qt 4.5.2 intrin.h 意外类型 "void" Visual Studio 2015